Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isn't S a literal type?

This code doesn't compile in Coliru, although it seems to comply with iso §5.19 p2 9th bullet point and iso §3.9 p10, i.e., S is a literal type and so, S a(1); should be considered a constant expression. In particular, iso §3.9 p10 bullet point 3 doesn't say anything about unitialized members.

#include <iostream>

struct S
{
    int i;
    float x;

    constexpr S(int j) : i{j} {}
};    

int main()
{
    constexpr S a(1);
}
like image 458
Wake up Brazil Avatar asked Jan 12 '23 15:01

Wake up Brazil


1 Answers

This is standardized in 7.1.5/4 ([dcl.constexpr], "The constexpr specifier"):

The definition of a constexpr constructor shall satisfy the following constraints:

  • [...]

  • every non-static data member and base class sub-object shall be initialized

  • [...]

like image 148
Kerrek SB Avatar answered Jan 17 '23 06:01

Kerrek SB