dimanche 1 mars 2015

enable_if + variadic templates: is it UB or a MSVC bug?

GCC 4.9.2, clang 3.5.0 and MSVC 19 (x86) compile the following as expected:



#include <iostream>
#include <vector>
#include <tuple>
#include <type_traits>

using namespace std;

template<size_t I = 0, typename... Tp>
inline typename enable_if<I == sizeof...(Tp), void>::type
tuple_for_each(tuple<Tp...> &)
{
cout << sizeof...(Tp) << endl;
}

template<size_t I = 0, typename... Tp>
inline typename enable_if<I < sizeof...(Tp), void>::type
tuple_for_each(tuple<Tp...> & t)
{
tuple_for_each<I + 1, Tp...>(t);
}

int main()
{
auto t = make_tuple(vector<int>(3), vector<double>(4));
tuple_for_each(t);
}


MSVC 18 (x64) instead reports the following:



Microsoft (R) C/C++ Optimizing Compiler Version 18.00.31101 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

main.cpp
main.cpp(19) : error C2770: invalid explicit template argument(s) for 'enable_if<I<0x01,void>::type tuple_for_each(std::tuple<_Types1...> &)
'
main.cpp(17) : see declaration of 'tuple_for_each'
main.cpp(25) : see reference to function template instantiation 'void tuple_for_each<0x00,std::vector<int,std::allocator<_Ty>>,std:: vector<double,std::allocator<double>>>(std::tuple<std::vector<_Ty,std::allocator<_Ty>>,std::vector<double,std::allocator<double>>> &)' being
compiled
with
[
_Ty=int
]
main.cpp(19) : error C2893: Failed to specialize function template 'enable_if<I==0x01,void>::type tuple_for_each(std::tuple<_Types1...> &)'
With the following template arguments:
'I=0x01'
'Tp={std::vector<int,std::allocator<_Ty>>, std::vector<double,std::allocator<_Ty>>}'


Is it UB somewhere or just a compiler bug? (If the latter, any workarounds?)


Aucun commentaire:

Enregistrer un commentaire