c++ - gcc 4.7 中的错误?与否,我不确定所以我在这里问

标签 c++ gcc c++11

在此代码段中:

template<class T, int Min, int Max>
class Range
{
    const T min_ = Min; // works without const

    T max_ = Max;

};

我遇到了错误

non static const member in class without ctor

但是如果我在类中初始化它,这就无关紧要了。应该将其记录为错误吗?

最佳答案

是的。隐式构造函数包含这些初始值设定项,因此不会发生错误。 §12.1/6:

The implicitly-defined default constructor performs the set of initializations of the class that would be performed by a user-written default constructor for that class with no ctor-initializer (12.6.2) and an empty compound-statement. If that user-written default constructor would be ill-formed, the program is ill-formed.

和 12.6.2/8:

In a non-delegating constructor, if a given non-static data member or base class is not designated by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer) and the entity is not a virtual base class of an abstract class (10.4), then

— if the entity is a non-static data member that has a brace-or-equal-initializer, the entity is initialized as specified in 8.5;

关于c++ - gcc 4.7 中的错误?与否,我不确定所以我在这里问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10917875/

相关文章:

c++,对 ifstream 使用 get 和 >>

c++ - 在 Objective c++/c++ 中包装 Objective C

c++ - 删除链表中的所有节点

c - 警告 : data definition has no type or storage class

gcc - 将两个 GCC 编译的 .o 目标文件合并到第三个 .o 文件中

c++ - C++中使用自动说明符推导纯右值类型时的判别

c++ - C++11 标准容器是 "final"吗?

c++ - 如何对具有 2 个模板值的类中的函数仅使用 1 个模板值

c - 操作包含文件的搜索路径

c++ - 为什么 STL 的 std::sort 不适用于不可变类?