Wrap the type-specific code in either overloaded function or explicitly specialized template:
void doReadFromFile(Matrix_1D &a, int x, int y) { a->readFromFile(x); } void doReadFromFile(Matrix_2D &a, int x, int y) { a->readFromFile(x, y); } template <typename Matrix_xx> bool ProcessMatrix<Matrix_xx>::function1(Matrix_xx a) { int x, y; // ... some code here ... // doReadFromFile(a, x, y); }
If Matrix_xx is Matrix_1D, overloading will select the first overload, if it is Matrix_2D, overloading will select the second overload and if it's anything else, it won't compile. But if somebody provides new type of matrix, they can make it compile by defining the doReadFromFile for it.
This is generally useful trick and reason why standard library uses "traits"—they can be defined for class somebody gives you and they can be defined for non-class types. The "traits" can be either in the form of explicitly specialized templates or free functions, usually looked up with argument-dependent lookup (placed in the namespace of their argument, not the template).
For completeness, the explicit specialization would look like:
template <typename Matrix_xx> struct doReadFromFile {}; template <> struct<Matrix_1D> struct doReadFromFile { void operator()(Matrix_1D &a, int x, int y) { a->readFromFile(x); } } template <> struct<Matrix_1D> struct doReadFromFile { void operator()(Matrix_1D &a, int x, int y) { a->readFromFile(x, y); } }
enable_ifbut you'd like to have an "inline" solution.static_ifis the answer to your question, and it's a highly anticipated feature for C++.static_if, but from your reduced example, it's impossible to suggest anything general to you. The most general thing isstatic_ifwhich is not in the language yet.enable_ifwould be useful together with "has_method" trait to define the trait wrapping the method call for any class that has it, but the wrapper has to be defined in the first place anyway.