Constexpr non-static member function with non-constexpr constructor (gcc, clang are different)

For this code:

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

it compiles without warnings or errors using clang 3.6, 3.7 and 3.8 with -std=c++14 . But in g ++ 5.x the following errors occur:

 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 

Which compiler is right and why?

I reviewed the requirements in C ++ 14 [dcl.constexpr] / 3, which states that for a constexpr function "each of its parameter types must be a literal type", but this section does not explicitly mention the member of the function, and does not say whether the implied *this parameter for the purpose of this sentence.

+5
source share
1 answer

This is the main defect that has been fixed for C ++ 14

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

Clang has been fixed

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

There is a tracker for GCC, but it does not look like anyone else.

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

+6
source

Source: https://habr.com/ru/post/1246662/


All Articles