加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 我可以使用模板作为常量吗?

发布时间:2020-12-16 10:10:06 所属栏目:百科 来源:网络整理
导读:我想编写代码如下: templatetypename T const int a;template const int afloat=5;template const int adouble=14;template const int achar=6;template const int awchar=33; 解决方法 是的,如果您的编译器支持C 1y variable templates功能,则可以. templa
我想编写代码如下:

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 1y variable templates功能,则可以.

template<typename T> const int a = 0;

template<> const int a<float> = 5;
template<> const int a<double> = 14;
template<> const int a<char> = 6;
template<> const int a<wchar_t> = 33;

我在>之间添加了空格和=的特化,因为clang会遇到解析错误

error: a space is required between a right angle bracket and an equals sign (use ‘> =’)

Live demo

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读