gpt4 book ai didi

c++ - Visual Studio 2013 CTP 是否支持非整数类型的类内静态常量初始值设定项?

转载 作者:行者123 更新时间:2023-11-28 00:31:41 25 4
gpt4 key购买 nike

当使用 g++(和我上次检查的 clang++)时,我可以将其写在标题中并期望它能工作:

struct test
{
static constexpr float MEANING_OF_LIFE = 42.0f;
};

但是 Visual Studio 2013 CTP 需要这个(并且它会在没有警告或错误的情况下编译):

struct test
{
static const float MEANING_OF_LIFE;
};
const float test::MEANING_OF_LIFE = 42.0f; // <- In the header, not a .cpp file.

这是 Visual Studio 的缺陷还是我做错了什么?

最佳答案

第二种情况是错误的:它需要是

struct test
{
static const float MEANING_OF_LIFE;
};

const float test::MEANING_OF_LIFE = 42.0f; // In an implementation file

实际定义需要放在实现 (.cpp) 文件中,而不是放在 header 中,否则您将违反 ODR。

第一种情况是有效的 C++11,但我猜 Visual Studio 还不支持它。 (如果您使用整数类型而不是 float,它可能会起作用,因为 C++98 也允许这样做。)

关于c++ - Visual Studio 2013 CTP 是否支持非整数类型的类内静态常量初始值设定项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22654603/

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