问题
Following up with this question Having a constexpr static string gives a linker error
In the question, this code wasn't able to compile:
#include <iostream>
struct Test { static constexpr char text[] = "Text"; };
int main()
{
std::cout << Test::text << std::endl; // error: undefined reference to `Test::text'
}
From the comment, this code is able to compile:
#include <iostream>
struct Test { static constexpr auto text = "Text"; };
int main()
{
std::cout << Test::text << std::endl;
}
My question is why the auto
version works but the array of char
version doesn't?
Could you please point out the statement in the standard allowing the second version and disallowing the first?
I took a look at Strange behavior with constexpr static member variable but it seems to be another question.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/34793392/different-behavior-observed-with-constexpr-auto-char-array-variable