I am having a problem with the constexpr static element of a template structure. The code compiles, but I get a binding error. Here is what I am trying to do:
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; }
I had communication errors with this, so I tried this:
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; }
But instead, I got this strange error:
error: redefinition of 'invoke' with a different type: 'const decltype(Test<n>::invoke)' vs 'const std::tuple<int, const char *, double>'
Another type? Obviously, the non-template version works just fine:
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; }
How can I make the template version work? Thank you very much
source share