gpt4 book ai didi

c++ - 使用 MSVC 2013 初始化 `static constexpr double`

转载 作者:可可西里 更新时间:2023-11-01 18:35:48 24 4
gpt4 key购买 nike

标题说明了一切,两种常用方法都行不通。我错过了什么?

1.

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

(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' : 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 编译器。

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

最佳答案

根据 Stephan T. L. 在这个 blog 中的表格和解释,constexpr 实际上仅在 VS Nov 2013 CTP 中部分实现。

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.

(想放在评论里,但是积分不够)

编辑:补充一下,在 Herb 的 blog 中, 关于静态成员有几乎相同的问题,但答复与斯蒂芬相同。

我认为可以简单地说 2013 年 11 月 CTP 未实现所需的 OP 功能(发送错误报告?)并等待 2014 年 7 月 CTP 或 VS Next(遗憾)。

关于c++ - 使用 MSVC 2013 初始化 `static constexpr double`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23454507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com