Żądam static constexpr
tablicę klasowych elementów wewnątrz matrycy klasy podobny do następującego kodu:constexpr statyczne członek szablon tablica rekurencyjnie wymagane przez siebie
struct Element {
unsigned i;
constexpr Element (unsigned i) : i(i) { }
};
template <bool Reverse>
struct Template {
static constexpr Element element[] = {
Element (Reverse ? 1 : 0),
Element (Reverse ? 0 : 1),
};
};
int main (int argc, char **argv) {
return Template<true>::element[0].i;
}
Oczywiście rzeczywista Element
struktura jest bardziej złożona niż w ten przykład, ale już pokazuje problem. Jeśli mogę skompilować ten dowcip gcc
pojawia się błąd o rekurencyjnej zależności:
test.cc: In instantiation of ‘constexpr Element Template<true>::element [2]’:
test.cc:11:27: recursively required from ‘constexpr Element Template<true>::element [2]’
test.cc:11:27: required from ‘constexpr Element Template<true>::element [2]’
test.cc:20:2: required from here
test.cc:11:27: fatal error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum)
static constexpr Element element[] = {
^
compilation terminated.
Przede wszystkim jestem ciekawy, jak obejść ten błąd, ale będzie również polana, czy mogę dostać podpowiedź do przyczyny tego czy dlaczego taka konstrukcja nie powinna być ważna ...
Jaką wersję GCC masz? Nie mogę odtworzyć z żadnym kompilatorem. – DeiDei
Testowałem 'g ++ - 5.4.0'. Właśnie zauważyłem, że kompilator nie zawiedzie, jeśli użyję tylko fragmentu z góry. Muszę później użyć 'Template :: element' .. –
Jonas
Wygląda jak błąd kompilatora. W celu obejścia problemu [to działa] (https://godbolt.org/g/yDMmWt) z 'std :: array'. –