Mam problem z statycznym członkiem constexpr szablonu struct. Kod się kompiluje, ale pojawia się błąd łączenia. Oto, co usiłuję zrobić:Niezdefiniowane odwołanie, szablon struct i constexpr static member
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;
}
mam łączenie błędy z tym, więc próbowałem to:
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;
}
Ale zamiast tego mam ten dziwny błąd:
error: redefinition of 'invoke' with a different type: 'const decltype(Test<n>::invoke)' vs 'const std::tuple<int, const char *, double>'
Inny typ? Oczywiście, wersja non-szablon działa dobrze:
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;
}
Jak mogę dostać wersję szablon do pracy? Dziękuję bardzo
Dodałem C++ 14, ponieważ 'make_tuple' nie jest constexpr do tego czasu. –