constexpr非静态成员函数与非constexpr构造函数(gcc,clang不同)

M.M*_*M.M 8 c++ gcc clang constexpr c++14

对于此代码:

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

    int m;
};

int main() { S s(1); }
Run Code Online (Sandbox Code Playgroud)

它是由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
Run Code Online (Sandbox Code Playgroud)

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

我查看了C++ 14 [dcl.constexpr]/3中的要求,它说对于constexpr函数"它的每个参数类型应该是文字类型",但该部分没有明确提到成员函数,并且没有说*this为了本条款的目的,隐含是否作为参数计算.

use*_*267 8

这是C++ 14修复的核心缺陷

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1684

Clang被打了补丁

https://groups.google.com/a/isocpp.org/forum/#!topic/std-discussion/6jM8M8FUs30

GCC有一个跟踪器,但它看起来并没有人解决它

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66297

  • 有关信息:这已在 gcc 7.2 中修复,大约在 2017 年 8 月(参见 https://gcc.gnu.org/viewcvs/gcc?view=revision&revision=248752) (2认同)