Ho riscontrato qualche problema con un membro constexpr statico di una struttura template. Il codice viene compilato ma ricevo un errore di collegamento. Ecco quello che sto cercando di fare:Riferimento non definito, template struct e membro statico di constexpr
template<int n>
struct Test {
static constexpr auto invoke = make_tuple(2, "test", 3.4);
};
template<typename T>
void test(T&& t) {
cout << t << endl;
}
int main() {
test(get<0>(Test<2>::invoke));
return 0;
}
mi sono collegando gli errori con questo, quindi ho provato questo:
template<int n>
struct Test {
static constexpr auto invoke = make_tuple(2, "test", 3.4);
};
// declare it outside the class
template<int n>
constexpr decltype(Test<n>::invoke) Test<n>::invoke;
template<typename T>
void test(T&& t) {
cout << t << endl;
}
int main() {
test(get<0>(Test<2>::invoke));
return 0;
}
Ma invece ho ottenuto questo strano errore:
error: redefinition of 'invoke' with a different type: 'const decltype(Test<n>::invoke)' vs 'const std::tuple<int, const char *, double>'
Un tipo diverso ?? Ovviamente, la versione non-modello funziona bene:
struct Test {
static constexpr auto invoke = make_tuple(2, "test", 3.4);
};
constexpr decltype(Test::invoke) Test::invoke;
template<typename T>
void test(T&& t) {
cout << t << endl;
}
int main() {
test(get<0>(Test::invoke));
return 0;
}
Come posso ottenere la versione modello di lavorare? Grazie mille
Ho aggiunto C++ 14 poiché 'make_tuple' non è constexpr fino ad allora. –