我几乎可以肯定这个问题已经在某个地方得到了回答,但我找不到它,所以我只是问一下。
编译正常
template <int SIZE, unsigned int NUMSYNC>
class MyClass{
private:
std::uniform_int_distribution<int> randomNumberDistribution{ 0, SIZE };
}
无法编译(常量 SIZE 不是类型名称)
template <int SIZE, unsigned int NUMSYNC>
class MyClass{
private:
std::uniform_int_distribution<int> randomNumberDistribution( 0, SIZE );
}
我试图理解大括号初始化与传统初始化的区别以及为什么会起作用。
我正在使用 C++14 进行编译
最佳答案
这是由于标准中的定义造成的。否则,编译器在解析代码时可能无法将其与成员函数声明区分开。
2) Through a default member initializer, which is a brace or equals initializer included in the member declaration and is used if the member is omitted from the member initializer list of a constructor. (Emphasis by me)
关于使用模板参数的 C++ 大括号初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60652741/