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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With