I am trying to define a class template with non-templated member function f()
, where f() is allowed to call only when the template agument of the class is int
.
#include <type_traits>
using namespace std;
template<typename T>
struct MyClass
{
T f()
{
typename enable_if< is_same<T,int>::value >::type();
return 0;
};
};
int main()
{
MyClass<int> x;
MyClass<double> y;
x.f();
y.f(); // generates compile-time error
}
It works, but is it a good idea, or it is better to use the regular solutions ?
std::enable_if to conditionally compile a member function
usage of enable_if for non-templated member function
Selecting a member function using different enable_if conditions
Are there alternative methods ?
Aucun commentaire:
Enregistrer un commentaire