初始化一个`static constexpr double` [英] Initializing a `static constexpr double`

查看:823
本文介绍了初始化一个`static constexpr double`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题说这一切,和通常的方式都不工作。我缺少什么?

Title says it all, and both of the usual ways do not work. What am I missing?

1。

class Cl {
    static constexpr double PI;
};
constexpr double Cl::PI = 3.14;




(26):error C2737:'private:static double const
Cl :: PI':'constexpr'对象必须初始化

(26): error C2737: 'private: static double const Cl::PI' : 'constexpr' object must be initialized

2。

class Cl {
    static constexpr double PI = 3.14;
};




(26):error C2864:'Cl :: PI':带有类内初始化器的静态
数据成员必须具有非易失性const
整数类型

类型是'const double'

(26): error C2864: 'Cl::PI' : a static data member with an in-class initializer must have non-volatile const integral type
type is 'const double'

在两次尝试中,错误都在类中的同一行。我使用VisualStudio / MSVC 2013年11月的CTP编译器。

In both attempts, the error is on the same line inside the class. I am using the VisualStudio/MSVC Nov 2013 CTP compiler.

请注意,使变量 const 不是一个解决方案因为我想在constexpr函数和正常函数中使用这个常量。

Note that making the variable const is not a solution because I want to use this constant in both constexpr functions and normal functions.

推荐答案

博客 constexpr 确实只在2013年11月的CTP中部分实现。

By the tables and explanation from Stephan T. L. in this blog, the constexpr is indeed only partially implemented in VS Nov 2013 CTP.


CTP支持C ++ 11 constexpr,除了成员函数。
(另一个限制是不支持数组。)而且,
不支持C ++ 14的扩展constexpr规则。

The CTP supports C++11 constexpr, except for member functions. (Another limitation is that arrays aren't supported.) Also, it doesn't support C++14's extended constexpr rules.

(希望放在评论中,但还没有足够的分数)

(wish to put it in comments, but no sufficient points yet)

编辑:只需添加,在Herb的博客,有关于静态的近同样的问题成员,但回复与Stephan相同。

Just to add, in Herb's blog, there is near same question about static members, but the reply is the same as Stephan.

我认为这是安全简单的说,2013年11月CTP不实现所需的OP功能(发送错误报告? ),并等待2014年7月的CTP或VS Next(可悲)。

I think it is safe to simple say that Nov 2013 CTP not implement the required OP feature (send a bug report?) and wait for a Jul 2014 CTP or VS Next (sadly).

这篇关于初始化一个`static constexpr double`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆