如果我有一些琐碎的事情,例如(澄清一下,我并不是说这是一个很好的实现,只是一个演示成员函数部分模板特化失败的例子):
template <typename T, typename U>
class BankAccount
{
T money;
U interestRate;
public:
BankAccount(T money, U interestRate) :
money(money), interestRate(interestRate)
{}
void showMeTheMoney();
};
我不能能够通过以下方式专门化每个功能:
// invalid code
template <typename U>
void BankAccount <int, U>::showMeTheMoney()
{
printf("$%d.00 interest: %f\n", money, interestRate);
}
template <typename U>
void BankAccount <long, U>::showMeTheMoney()
{
printf("$%l.00 interest: %f\n", money, interestRate);
}
template <typename U>
void BankAccount <float, U>::showMeTheMoney()
{
printf("$%.2f interest: %f\n", money, interestRate);
}
template <typename U>
void BankAccount <double, U>::showMeTheMoney()
{
printf("$%.2f interest: %f\n", money, interestRate);
}
int main(int argc, char *argv[])
{
BankAccount<double, float> b(500, 0.03);
b.showMeTheMoney();
BankAccount<std::uint64_t, float> c(1234, 0.01);
c.showMeTheMoney();
}
等不幸的是,C++ 标准不允许这样做:
14.5.5.3.1. The template parameter list of a member of a class template partial specialization shall match the template parameter list of the class template partial specialization. The template argument list of a member of a class template partial specialization shall match the template argument list of the class template partial specialization.
因此,唯一的解决方案(据我所知)是使用类型特征或使用样板代码重现整个类。这个决定背后是否有理由,或者这是 C++ 中根本不存在的东西,因为没有足够的需求,还是其他一些原因?
最佳答案
因为它不是您编写的成员函数的模板特化。它是类(class)的特化。因此,代码应该如下所示(我添加了一个公共(public)基类,您不必为所有规范定义成员):
template <typename T, typename U>
class BankAccountBase
{
protected:
T money;
U interestRate;
public:
BankAccountBase(T money, U interestRate) :
money(money), interestRate(interestRate)
{}
};
template <typename T, typename U>
class BankAccount
{
T money;
U interestRate;
public:
BankAccount(T money, U interestRate) :
money(money), interestRate(interestRate)
{}
void showMeTheMoney();
};
template <typename U>
class BankAccount <int, U> : public BankAccountBase <int, U>
{
public:
BankAccount(int money, U interestRate) :BankAccountBase(money, interestRate) { }
void showMeTheMoney();
};
template <typename U>
class BankAccount <long, U> : public BankAccountBase <long, U>
{
public:
BankAccount(long money, U interestRate) :BankAccountBase(money, interestRate) { }
void showMeTheMoney();
};
template <typename U>
class BankAccount <float, U> : public BankAccountBase <float, U>
{
BankAccount(float money, U interestRate) :BankAccountBase(money, interestRate) { }
public:
void showMeTheMoney();
};
template <typename U>
class BankAccount <double, U> : public BankAccountBase <double, U>
{
public:
BankAccount(double money, U interestRate) :BankAccountBase(money, interestRate) { }
void showMeTheMoney();
};
template <typename U>
class BankAccount <long long, U> : public BankAccountBase <long long, U>
{
public:
BankAccount(long long money, U interestRate) :BankAccountBase(money, interestRate) { }
void showMeTheMoney();
};
// invalid code
template <typename U>
void BankAccount <int, U>::showMeTheMoney()
{
printf("$%d.00 interest: %f\n", money, interestRate);
}
template <typename U>
void BankAccount <long, U>::showMeTheMoney()
{
printf("$%l.00 interest: %f\n", money, interestRate);
}
template <typename U>
void BankAccount <long long, U>::showMeTheMoney()
{
printf("$%l.00 interest: %f\n", money, interestRate);
}
template <typename U>
void BankAccount <float, U>::showMeTheMoney()
{
printf("$%.2f interest: %f\n", money, interestRate);
}
template <typename U>
void BankAccount <double, U>::showMeTheMoney()
{
printf("$%.2f interest: %f\n", money, interestRate);
}
int main(int argc, char *argv[])
{
BankAccount<double, float> b(500, 0.03);
b.showMeTheMoney();
BankAccount<long long, float> c(1234, 0.01);
c.showMeTheMoney();
}
关于c++ - 为什么不允许成员函数的模板特化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53855546/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin