[英]May I use a constant number to choose a class at compile time, possibly using templates?
[英]May I use a template for a constant?
我想编写代码如下:
template<typename T> const int a;
template<> const int a<float>=5;
template<> const int a<double>=14;
template<> const int a<char>=6;
template<> const int a<wchar>=33;
适用于所有C ++版本的解决方案(包括C ++ 11之前的版本):
template<typename T>
struct a { static const int value; };
template<> const int a<float>::value = 5;
template<> const int a<double>::value = 14;
template<> const int a<char>::value = 6;
template<> const int a<wchar_t>::value = 33;
(注意问题是使用wchar
,这不是标准类型)
它有点笨拙,但有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.