Explanation behind C++ Quiz by Olve Maudal (alias template) -
the following code http://www.pvv.org/~oma/pubquiz_accu_apr2014.pdf (#6, solution on page 34). goal guess output following.
#include <iostream> template <template <typename> class> struct x { x() { std::cout << "1"; } }; template<typename > struct y {}; template<typename t> using z = y<t>; template<> struct x<y> { x() { std::cout << "2"; } }; int main() { x<y> x1; x<z> x2; } the answer can found on page 34. don’t understand second case alias template, why primary template chosen x<z> instead of specialized.
the correct answer should "21" written in presentaton. mingw (gcc 5.1) prints "22" , http://ideone.com (which uses gcc 4.9.2) prints "22". clang friend on macos x prints "21". guess bug in gcc.
can explain me why "1" printed x<z> , paragraph standard gcc might failed implement or not yet implemented?
i think is
14.5.7 alias templates
1 template-declaration in declaration alias-declaration (clause 7) declares identifier alias template. alias template name family of types. name of alias template template-name.
the above means y , z different template-names, different templates. might confuse you/the compiler y<t> , z<t> yield identical type.
consider:
#include <type_traits> template <typename> struct w {}; template <template <typename> class> struct x {}; template<typename> struct y {}; template<typename t> using z = y<t>; int main() { static_assert( std::is_same< y<int>, z<int> >::value, "oops" ); static_assert( std::is_same< w<y<int>>, w<z<int>> >::value, "oops" ); static_assert( ! std::is_same< x<y>, x<z> >::value, "oops" ); } the above works clang, not gcc.
edit: pointed out @t.c. there active cwg issue 1286 suggests clang doing standard says, not intended.
Comments
Post a Comment