このstd::vector<T>
クラスはSTLコンテナの概念のモデルであるため、vectorを適切に実装するには、ネストされたtypedefとを含める必要がありvalue_type
ますreference
。これは、SFINAEを使用して検出できるはずです。ただし、私自身のテストでは、SFINAEを使用してネストされたvalue_type
typedefを検出できますが、何らかの理由で検出できませんreference
。
template <class T>
typename T::value_type* test(T)
{
cout << "Has nested typedef!" << endl;
}
template <class T>
void test(...)
{
cout << "Doesn't have nested typedef!" << endl;
}
int main()
{
test(std::vector<int>());
}
これは以下を出力します:Has nested typedef!
ただし、次のように置き換えるvalue_type
とreference
、次のようになります。
template <class T>
typename T::reference* test(T)
{
cout << "Has nested typedef!" << endl;
}
template <class T>
void test(...)
{
cout << "Doesn't have nested typedef!" << endl;
}
int main()
{
test(std::vector<int>());
}
...プログラムはまったくコンパイルに失敗し、エラーが発生します:error: no matching function for call to test(std::vector<int, std::allocator<int> >)
SFINAEテクニックが機能するのに機能しないのはなぜT::value_type
ですかT::reference
?