Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I specialize std::tuple_element?

The following program attempts to provide a specialization for std::tuple_element for the user-defined type foo. Unfortunately, clang-3.5 rejects it with libc++, but using other compilers or using other standard libraries with clang-3.5 accept the program. Is it correct? If no, why not?

#include <utility>

struct foo {};

namespace std
{
    template<size_t, class> struct tuple_element;
    template<size_t i>
    struct tuple_element<i, foo> {};
}

int main()
{
    return 0;
}

Compiler output:

$ clang-3.5 -std=c++11 -stdlib=libc++ -lc++ test.cpp
test.cpp:11:8: error: explicit specialization of non-template struct 'tuple_element'
struct tuple_element<i, foo> {};
       ^            ~~~~~~~~
1 error generated.

$ clang-3.5 -std=c++11 -lstdc++ test.cpp
(no error)

$ g++-4.9 -std=c++11 test.cpp
(no error)
like image 329
Jared Hoberock Avatar asked Jul 23 '15 20:07

Jared Hoberock


1 Answers

libc++'s entities are actually in std::__1::, an inline namespace inside std. So your own forward declaration template<size_t, class> struct tuple_element; actually declares a different class template, and then the partial specialization blows up due to ambiguity (though the error message is misleading).

Remove the forward declaration and it should work.

like image 121
T.C. Avatar answered Nov 20 '22 01:11

T.C.