constexpr defaulted default constructors

我怕爱的太早我们不能终老 提交于 2020-12-01 09:51:24

问题


I get compiler error by Clang 3.8 and GCC 5.3 if I want to declare my default-ed default constructors as constexpr. According to this stackoverflow question it just should work fine:

struct A
{
    constexpr A() = default;

    int x;
};

however:

Error: defaulted definition of default constructor is not constexpr

Have you got any clue what is actually going on?


回答1:


As it stands, x remains uninitialized, so the object can not be constructed at compile time.

You need to initialize x:

struct A
{
    constexpr A() = default;

    int x = 1;
};


来源:https://stackoverflow.com/questions/36392326/constexpr-defaulted-default-constructors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!