c++ - Prevent templated member function from being instantiated for a given type -
i have templated matrix class explicitly instantiate various pod types , custom class types. of member functions don't make sense few of such custom types. example:
matrix<int> loadfile(....); // makes sense matrix<my_custom_class> loadfile(...); //this doesn't make sense in context of custom class
can prevent instantiation of loadfile
function (which member function) matrix
objects of select types? far have avoided issue making loadfile
friend function , explicitly controlling instantiation. want know if can when loadfile
member function of matrix
.
the first question whether need control this. happens if call member function on matrix stores my_custom_class
? can provide support in class (or template) member function work?
if want inhibit use of member functions particular type, can use specialization block particular instantiation:
template <typename t> struct test { void foo() {} }; template <> inline void test<int>::foo() = delete;
or add static_assert
s common implementation verifying preconditions types allowed or disallowed?
template <typename t> struct test { void foo() { static_assert(std::is_same<t,int>::value || std::is_same<t,double>::value, "only allowed int , double"); // regular code } };
Comments
Post a Comment