2016-04-05 40 views
6

GCC (5.3) & Clang (3.8), test'daki ilk satırın kötü olduğunu, ancak ikincisinin iyi olduğunu iddia eder. MSVC (2015.2) her ikisinin de geçersiz olduğunu söylüyor.constexpr template argüman tuhaflığı

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 
} 

Tam olarak, standarda göre, fark nedir? Bu kod ile başlamak yasal mıdır? İlk hat için


clang hatası:

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 hatası:

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 ...>' 
+0

Lütfen bize hangi hataları aldığınızı söyler misiniz? –

+0

Burada metnin duvarlarını yayınlamak için çok rahat değilim, ama burada bunun esası. MSVC altında ikinci satır: "'std :: get': eşleşen aşırı yüklenmiş fonksiyon bulunamadı". İlk satır: std :: tuple'nin içlerine işaret eden benzer çizgiler boyunca her zamanki mumbo-jumbo her türlü. – vpozdyayev

+1

@vpozdyayev Hatalar çok yardımcı oluyor - sorunun ne olduğunu anlamamı sağlayan hata mesajları. Bu sadece mumbo-jumbo değil. – Barry

cevap

6

tl; dr ben her iki durumda da gcc ve clang davranışı doğru olduğunu düşünüyorum.


İki çağrınız arasında ince bir fark var. bana sadece göstermek için bir takma ad ilave edelim:

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

Yazdığınız:

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

Zero() bir tip olduğunu. Bir Zero döndüren bir nullary işlevidir. Gibi, bir tür alır std::get sürümü diyorsunuz: std::get<T>(). Zero() türünde tuple öğesinde öğe olmadığından, bu bir hatadır. Öte yandan

, sen yazarken:

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

n değil türüdür - bu sadece hiç bir değerdir. std::integral_constant'un bir constexpr operator size_t() olması nedeniyle, hangisini kullanırsınız ve std::get<I>() numaralı telefonu arayarak sonuçta beklersiniz.

aynı basitçe yanı ayracı başlatma kullanarak başarılı olabilir: Zero{} beri

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

kesinlikle bir tür değil.

+1

Ah, iyi olmanın en çok uğraşan ayrıştırıcısı :) ... C++ son yıllarda bu kadar uygun olmaya başlamıştı. – vpozdyayev

+0

Aslında ... "std :: get (tuple);" satırı yalnızca Clang'da çalışır, ancak GCC değil ("n 'değeri sabit bir ifadede kullanılamaz"/"' n" bildirilmedi) 'constexpr' "). Bu bir GCC hatası mı? – vpozdyayev

+0

@vpozdyayev Çalışma durumunuzun, aslında geçerli bir kodun değil, kısaltılmış bir sürümü olması amaçlanmıştır. – Barry

İlgili konular