免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1831 | 回复: 0
打印 上一主题 下一主题

[C++] 类的静态字面量,double类型为何必须用constexpr而不是const? [复制链接]

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:58:11
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2017-03-09 11:30 |只看该作者 |倒序浏览
本帖最后由 asker160 于 2017-03-09 11:32 编辑

rt,一个很简单的程序,struct结构体里面定义了几个static变量,double类型的如果不同constexpr就不通过编译,而int类型的没有问题:

  1. #include<stdio.h>
  2. struct C{
  3.     static int i;
  4.     static const int j=1;
  5.     static constexpr double d=1;
  6.     static const double d1=1.0;
  7. };
  8. int main(){
  9.     return 0;
  10. }

复制代码
编译会有错误:

  1. testStatic.cpp:6:25: error: in-class initializer for static data member of
  2.       type 'const double' requires 'constexpr' specifier
  3.       [-Wstatic-float-init]
  4.     static const double d1=1.0;
  5.                         ^  ~~~
  6. testStatic.cpp:6:5: note: add 'constexpr'
  7.     static const double d1=1.0;
  8.     ^
  9.     constexpr
  10. 1 error generated.
复制代码

这个到底是什么情况,int可以const,double就得constexpr,语言这么设计的原因是?

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP