当我尝试float用作模板参数时,编译器会为此代码而烦恼,同时int工作正常.
是因为我不能float用作模板参数吗?
#include<iostream>
using namespace std;
template <class T, T defaultValue>
class GenericClass
{
private:
T value;
public:
GenericClass()
{
value = defaultValue;
}
T returnVal()
{
return value;
}
};
int main()
{
GenericClass <int, 10> gcInteger;
GenericClass < float, 4.6f> gcFlaot;
cout << "\n sum of integer is "<<gcInteger.returnVal();
cout << "\n sum of float is "<<gcFlaot.returnVal();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
main.cpp: In function `int main()':
main.cpp:25: error: `float' is not a …Run Code Online (Sandbox Code Playgroud)