Regarding the first question: why does V1 not work? SFINAE only applies when overload resolution is enabled - V1, however, causes an error at the point where A is created, long before foo() resolves the overload.
I assume that there are many possible implementations, which, most correctly, depends on the specific case. A general approach would be to defer part A , which is different for different types of templates for a helper class.
template <typename T> class A_Helper; template <> class A_Helper<int> { public: static void foo( int value ){ std::cout << "INT: " << value << std::endl; } }; template <> class A_Helper<double> { public: static void foo( double value ){ std::cout << "DOUBLE: " << value << std::endl; } }; template <typename T> class A { public: A( T a ) : a_(a) {} void foo(){ A_Helper<T>::foo(a_); } private: T a_; };
The rest of A can only be declared once in the general way - only parts that are different are deferred until assistant. Depending on your requirements, there are many possible options ...
source share