我想根據類模板參數來確定調用哪個版本的成員函數。我曾經嘗試這樣做:使用不同的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