[cfe-dev] Template instantiation fails in SFINAE context, but results in an error

Jonathan Sauer jonathan.sauer at gmx.de
Sat Aug 4 01:45:27 PDT 2012


Hello,

I'm not sure if the following program does not compile due to a bug in clang or due to me
misunderstanding SFINAE rules (most likely the latter):

template <typename T>
struct Traits {
    using type = typename T::type;
};

template <typename T>
int foo(...)
{
    return 0;
}

template <typename T>
int foo(char[sizeof(Traits<T>)])
{
    return 1;
}

struct Bar {
    typedef int type;
};

int main()
{
    return foo<int>(0);
    //return foo<Bar>(0);
}


I would expect this program to compile and, when run, return 0, as the second version of
<foo> cannot be used due to <int> not having a member <type> when instantiating the <Traits>
template inside a SFINAE context. Instead I get the following error (clang r160613):

% clang++ -std=c++11 RandomTest.cpp
RandomTest.cpp:3:27: error: type 'int' cannot be used prior to '::' because it has no members
    using type = typename T::type;
                          ^
RandomTest.cpp:13:14: note: in instantiation of template class 'Traits<int>' requested here
int foo(char[sizeof(Traits<T>)])
             ^
RandomTest.cpp:13:5: note: while substituting explicitly-specified template arguments into function
      template 'foo'
int foo(char[sizeof(Traits<T>)])
    ^
1 error generated.


I don't understand why this error happens. Can anybody help me out, please?


With many thanks in advance,
Jonathan





More information about the cfe-dev mailing list