c++ - 具有非 constexpr 构造函数的 constexpr 非静态成员函数(gcc、clang 不同)

标签 c++ gcc clang c++14 constexpr

对于这段代码:

struct S
{
    S(int m): m(m) {}
    constexpr int f() const { return m; }

    int m;
};

int main() { S s(1); }

它由 clang 3.6、3.7 和 3.8 使用 -std=c++14 编译,没有警告或错误。但在 g++ 5.x 中出现以下错误:

main.cpp:4:19: error: enclosing class of constexpr non-static member function 'int S::f() const' is not a literal type
     constexpr int f() const { return m; }
               ^
main.cpp:1:8: note: 'S' is not literal because:
 struct S
        ^
main.cpp:1:8: note:   'S' is not an aggregate, does not have a trivial default constructor, and has no constexpr constructor that is not a copy or move constructor

哪个编译器是正确的,为什么?

我查看了 C++14 [dcl.constexpr]/3 中的要求,它说对于 constexpr 函数“它的每个参数类型都应该是文字类型”,但是那个部分没有明确提及成员函数,也没有说明隐含的 *this 是否算作本子句的参数。

最佳答案

关于c++ - 具有非 constexpr 构造函数的 constexpr 非静态成员函数(gcc、clang 不同),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36489369/

相关文章:

c++ - 指向模板类的函数指针

c - C 中的微小链表错误

c++ - gcc 发出的这个越界警告是错误的吗?

xcode - 可以告诉clang不要分析某些文件吗?

c++ - 关于将指向数组的指针作为函数参数的困惑

c++ - CMake 源文件找不到头文件

c++ - 将 3x1 或 1x3 cv::Mat 转换为 cv::Point3d?

c - 可变参数宏 : expansion of pasted tokens

c++ - Xcode 推断命名空间?

c++ - 无法验证我的父类(super class)或基类是否是宏或模板所声称的那样