クラステンプレートパラメータに基づいて、どのバージョンのメンバー関数が呼び出されるかを判断しようとしています。私はこれを試しました:
#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();
}
これは、「Tがintの場合は最初のMyFunctionを使用し、Tがintでない場合は2番目のMyFunctionを使用する」と思っていましたが、「エラー:「structstd::enable_if」に「type」という名前の型がありません」というコンパイラエラーが発生します。 。ここで私が間違っていることを誰かが指摘できますか?