Different behavior observed with constexpr auto/char-array variable

≡放荡痞女 提交于 2019-12-05 18:43:51

A declaration of a static data member in class is never a definition.
The difference between your examples is that only one requires a definition of text.

The auto version deduces char const*, hence text is only subject to an lvalue-to-rvalue conversion and not odr-used. By contrast, the first code effectively passes text's address, odr-use-ing it - i.e. necessitating a definition.

struct Test { static constexpr auto text = "Text"; };

resolves to

struct Test { static constexpr const char * text = "Text"; };

So the second expression is a constexpr value of a pointer not an array.

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