visual studio - c++ template enable_if unable to match function definition to an existing declaration -
i trying define template member function template class outside of class leverage sfinae partially overload function. minimal example of trying is:
test.h:
template<typename t, size_t d> class test { public: test(){} ~test(){} template<size_t w = d, typename = int*> void do_something(test&); private: t data[d]; }; #include <type_traits> template<typename t, size_t d> template<size_t w, typename std::enable_if<w == 2, int>::type* = 0> inline void test<t, d>::do_something(test &) { exit(2); } template<typename t, size_t d> template<size_t w, typename std::enable_if<w == 3, int>::type* = 0> inline void test<t, d>::do_something(test &) { exit(3); }
main.cpp:
int main(int, char**) { test<float, 2> t1; test<float, 2> t2; t1.do_something(t2); return 0; }
however code sample produces error: c2244 'test::do_something': unable match function definition existing declaration. if change
template<size_t w, typename std::enable_if<w == 2, int>::type* = 0>
to
template<size_t w, typename type>
and remove other definition of do_something code compile no problem know enable_if problem. question is: how use enable_if achieve partial overload effect without defining function inside class?
should add compiling msvs 2015.
you have use std::enable_if
in declaration:
template<typename t, std::size_t d> class test { public: template<std::size_t w, typename std::enable_if<w == 2>::type* = nullptr> void do_something(test<t, w> &); template<std::size_t w, typename std::enable_if<w == 3>::type* = nullptr> void do_something(test<t, w> &); }; template<typename t, std::size_t d> template<std::size_t w, typename std::enable_if<w == 2>::type*> void test<t, d>::do_something(test<t, w> &) { std::cout << 1 << std::endl; } template<typename t, std::size_t d> template<std::size_t w, typename std::enable_if<w == 3>::type*> void test<t, d>::do_something(test<t, w> &) { std::cout << 2 << std::endl; }
Comments
Post a Comment