我想根据类模板参数来确定调用哪个版本的成员函数。我曾经尝试这样做:使用不同的enable_if条件选择成员函数
#include <iostream>
#include <type_traits>
template<typename T>
struct Point
{
void MyFunction(typename std::enable_if<std::is_same<T, int>::value, T >::type* = 0)
{
std::cout << "T is int." << std::endl;
}
void MyFunction(typename std::enable_if<!std::is_same<T, int>::value, float >::type* = 0)
{
std::cout << "T is not int." << std::endl;
}
};
int main()
{
Point<int> intPoint;
intPoint.MyFunction();
Point<float> floatPoint;
floatPoint.MyFunction();
}
而且我认为他是说:“使用第一MyFunction的,如果T是int和使用第二的MyFunction如果T不是整数,但我得到的编译器错误说”错误:无类型命名'type'in'struct std :: enable_if'“。任何人都可以指出我在这里做错了什么吗?
相关Q&A:“发生了什么事我SFINAE”(终极版)(HTTP:/ /stackoverflow.com/questions/11531989/what-happened-to-my-sfinae-redux-conditional-template-class-members) – HostileFork