Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constexpr template argument weirdness

GCC (5.3) & Clang (3.8) claim that the first line in test is bad, but the second one is fine. MSVC (2015.2) says, both are invalid.

template< typename N, typename T >
void f( N n, T t ) { std::get< n >( t ); }
void test() {
    std::get< std::integral_constant< size_t, 0 >() >( std::make_tuple( 123 ) ); // not ok
    f( std::integral_constant< size_t, 0 >(), std::make_tuple( 123 ) ); // ok for gcc, clang, but not msvc
}

What, exactly, according to the standard, is the difference? Is this code legal to begin with?


clang error for the first line:

In file included from main.cpp:2:
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.3.0/../../../../include/c++/5.3.0/tuple:874:34: error: no matching function for call to '__get_helper2'
    { return std::forward<_Tp&&>(std::__get_helper2<_Tp>(__t)); }
                                 ^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:10:10: note: in instantiation of function template specialization 'std::get<std::integral_constant<unsigned long, 0> (), int>' requested here
    std::get<std::integral_constant<size_t, 0>()>( std::make_tuple( 123 ) ); // not ok
         ^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.3.0/../../../../include/c++/5.3.0/tuple:856:5: note: candidate template ignored: could not match '_Tuple_impl' against 'tuple'
    __get_helper2(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
    ^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.3.0/../../../../include/c++/5.3.0/tuple:861:5: note: candidate template ignored: could not match '_Tuple_impl' against 'tuple'
    __get_helper2(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
    ^
1 error generated.

gcc error:

In file included from main.cpp:2:0:
/usr/local/include/c++/5.3.0/tuple: In instantiation of 'constexpr _Tp&& std::get(std::tuple<_Elements ...>&&) [with _Tp = std::integral_constant<long unsigned int, 0ul>(); _Types = {int}]':
main.cpp:10:75:   required from here
/usr/local/include/c++/5.3.0/tuple:874:57: error: no matching function for call to '__get_helper2(std::tuple<int>&)'
     { return std::forward<_Tp&&>(std::__get_helper2<_Tp>(__t)); }
                                                         ^
/usr/local/include/c++/5.3.0/tuple:856:5: note: candidate: template<class _Head, long unsigned int __i, class ... _Tail> constexpr _Head& std::__get_helper2(std::_Tuple_impl<__i, _Head, _Tail ...>&)
     __get_helper2(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
     ^
/usr/local/include/c++/5.3.0/tuple:856:5: note:   template argument deduction/substitution failed:
/usr/local/include/c++/5.3.0/tuple:874:57: note:   mismatched types 'std::integral_constant<long unsigned int, 0ul>()' and 'int'
     { return std::forward<_Tp&&>(std::__get_helper2<_Tp>(__t)); }
                                                         ^
/usr/local/include/c++/5.3.0/tuple:874:57: note:   'std::tuple<int>' is not derived from 'std::_Tuple_impl<__i, std::integral_constant<long unsigned int, 0ul>(), _Tail ...>'
/usr/local/include/c++/5.3.0/tuple:861:5: note: candidate: template<class _Head, long unsigned int __i, class ... _Tail> constexpr const _Head& std::__get_helper2(const std::_Tuple_impl<__i, _Head, _Tail ...>&)
     __get_helper2(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
     ^
/usr/local/include/c++/5.3.0/tuple:861:5: note:   template argument deduction/substitution failed:
/usr/local/include/c++/5.3.0/tuple:874:57: note:   mismatched types 'std::integral_constant<long unsigned int, 0ul>()' and 'int'
     { return std::forward<_Tp&&>(std::__get_helper2<_Tp>(__t)); }
                                                         ^
/usr/local/include/c++/5.3.0/tuple:874:57: note:   'std::tuple<int>' is not derived from 'const std::_Tuple_impl<__i, std::integral_constant<long unsigned int, 0ul>(), _Tail ...>'
like image 347
vpozdyayev Avatar asked Apr 05 '16 14:04

vpozdyayev


1 Answers

tl;dr I think gcc and clang's behavior in both cases is correct.


There is a subtle difference between your two calls. Let me just add an alias to illustrate:

using Zero = std::integral_constant<size_t, 0>;
auto tuple = std::make_tuple(123);

When you write:

std::get<Zero()>(tuple);

Zero() is a type. It is a nullary function returning a Zero. As such, you're calling the version of std::get that takes a type: std::get<T>(). Since there is no element in tuple that has type Zero(), this is an error.

On the other hand, when you write:

Zero n;
std::get<n>(tuple);

n is not a type - it is only ever a value. Since std::integral_constant has a constexpr operator size_t(), that one gets used and you end up calling std::get<I>(), which does what you'd expect.

The same could be accomplished by simply using brace initialization as well:

std::get<Zero{}>(tuple);

since Zero{} is definitely not a type.

like image 148
Barry Avatar answered Sep 20 '22 00:09

Barry