Mam problemy z następującego kodu:zagnieżdżone struct przerwy constexpr pomimo identycznych do tych globalnych
template<typename T>
constexpr int get(T vec) {
return vec.get();
}
struct coord {
constexpr int get() const { return x; }
int x;
};
struct foo {
struct coord2 {
constexpr int get() const { return x; }
int x;
};
constexpr static coord f = { 5 };
constexpr static int g = get(f); // works
constexpr static coord2 h = { 5 };
constexpr static int i = get(h); // doesn't work
};
constexpr coord foo::f;
constexpr foo::coord2 foo::h;
int main(){}
Zasadniczo get(f)
uważany jest za stałą ekspresję, ale get(h)
nie jest. Zmieniło się tylko to, że używa się globalnej struktury coord
, a druga używa zagnieżdżonej struktury coord2
. Elementy struktury " są identyczne.
Dlaczego tak jest? Błąd
GCC:
błądtest.cpp:20:35: error: field initializer is not constant
Clang:
test.cpp:20:26: error: constexpr variable 'i' must be initialized by a constant expression
constexpr static int i = get(h); // doesn't work
^ ~~~~~~
test.cpp:8:10: note: undefined function 'get' cannot be used in a constant expression
return vec.get();
^
test.cpp:20:30: note: in call to 'get({5})'
constexpr static int i = get(h); // doesn't work
^
test.cpp:13:21: note: declared here
constexpr int get() const { return x; }
@dyp: Czy to nie jest to, co powiedziałem? (Jest to wnioskowane, nie jest jednoznaczne) –
Ach, OK, źle zrozumiałem ten akapit, przepraszam. – dyp