Is this a bug? Constexpr constructor silently becomes non-constexpr

纵饮孤独 提交于 2019-12-04 17:10:03

问题


Look at this code:

struct NonConstexpr {
    NonConstexpr() { }
};

template <typename T>
struct Bar {
    NonConstexpr nonConstexpr;

    constexpr Bar() { }
};

struct Foo {
    Bar<void> bar;

    constexpr Foo() { }
};

Foo has a member, Foo::bar::nonConstexpr, which has a non-constexpr constructor. So, my expectation is that this should not compile. But it compiles with gcc, clang and msvc. Is this a compiler bug, or some rule allows this code to compile?

If I add a NonConstexpr member into Foo directly, the code doesn't compile anymore.

(I got this problem, because I've expected static initialization for a global Foo object, but it got dynamically initialized, and it caused a problem, because of "static initialization order fiasco")


回答1:


Is this a compiler bug, or some rule allows this code to compile?

The rule that allows this to compile is:

10.1.5 The constexpr specifier [dcl.constexpr]
...
6. If the instantiated template specialization of a constexpr function template or member function of a class template would fail to satisfy the requirements for a constexpr function or constexpr constructor, that specialization is still a constexpr function or constexpr constructor, even though a call to such a function cannot appear in a constant expression. If no specialization of the template would satisfy the requirements for a constexpr function or constexpr constructor when considered as a non-template function or constructor, the template is ill-formed, no diagnostic required.

The above quote is taken from CPP standard draft N4713.


From the quote it may not be clear how Bar<void>'s constructor can appear in Foo's constructor as Foo's constructor is constexpr. But as noted in the comments, constexpr is not the same as constant expression. Foo's constructor is is not an expression, much less a constant expression.



来源:https://stackoverflow.com/questions/53630837/is-this-a-bug-constexpr-constructor-silently-becomes-non-constexpr

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