Следующий код компилируется и прекрасно работает в gcc и clang, но не компилируется в Visual Studio 2017.7 (x86-64):
#include <vector>
#include <iostream>
#include <type_traits>
template <template <typename...> class>
struct isVector : public std::false_type
{ };
template <>
struct isVector<std::vector> : public std::true_type
{ };
// Other isVector specializations (for QVector<T>, etc...)
// ...
// A function accepting vector<vector<double>>
template <
template<typename ...> class V1,
template<typename ...> class V2 >
auto loadData(V1<V2<double>> & v, std::string fn)
-> std::enable_if_t<isVector<V1>::value && isVector<V2>::value>
{
std::cout << "- vector of vector version, " << fn << std::endl;
}
// A function accepting vector<double>
template <
template<typename ...> class V >
auto loadData(V<double> & v, std::string fn)
-> std::enable_if_t<isVector<V>::value>
{
std::cout << "- vector version, " << fn << std::endl;
V<V<double>> vv({ v });
loadData(vv, fn);
}
// Other loadData() function specializations and overloads
// ...
int main()
{
std::vector<double> vd;
std::vector<std::vector<double>> vvd;
loadData(vd, "case 1");
loadData(vvd, "case 2");
return 0;
}
Это сообщение об ошибке:
<source>(50): error C2672: 'loadData': no matching overloaded function found
<source>(50): error C2784: 'enable_if<_Test,_Ty>::type loadData(V<double> &,std::string)': could not deduce template argument for 'V<double> &' from 'std::vector<double,std::allocator<_Ty>>'
with
[
_Ty=void
]
and
[
_Ty=double
]
<source>(31): note: see declaration of 'loadData'
<source>(50): error C2782: 'enable_if<_Test,_Ty>::type loadData(V<double> &,std::string)': template parameter 'V' is ambiguous
with
[
_Ty=void
]
<source>(31): note: see declaration of 'loadData'
<source>(50): note: could be 'std::_Vector_alloc'
<source>(50): note: or 'std::vector'
<source>(50): error C2784: 'enable_if<_Test,_Ty>::type loadData(V1<V2<double>> &,std::string)': could not deduce template argument for 'V1<V2<double>> &' from 'std::vector<double,std::allocator<_Ty>>'
with
[
_Ty=void
]
and
[
_Ty=double
]
<source>(22): note: see declaration of 'loadData'
<source>(50): error C2782: 'enable_if<_Test,_Ty>::type loadData(V1<V2<double>> &,std::string)': template parameter 'V1' is ambiguous
with
[
_Ty=void
]
<source>(22): note: see declaration of 'loadData'
<source>(50): note: could be 'std::_Vector_alloc'
<source>(50): note: or 'std::vector'
<source>(51): error C2672: 'loadData': no matching overloaded function found
<source>(51): error C2784: 'enable_if<_Test,_Ty>::type loadData(V<double> &,std::string)': could not deduce template argument for 'V<double> &' from 'std::vector<std::vector<double,std::allocator<_Ty>>,std::allocator<std::vector<_Ty,std::allocator<_Ty>>>>'
with
[
_Ty=void
]
and
[
_Ty=double
]
<source>(31): note: see declaration of 'loadData'
<source>(51): error C2782: 'enable_if<_Test,_Ty>::type loadData(V<double> &,std::string)': template parameter 'V' is ambiguous
with
[
_Ty=void
]
<source>(31): note: see declaration of 'loadData'
<source>(51): note: could be 'std::_Vector_alloc'
<source>(51): note: or 'std::vector'
<source>(51): error C2784: 'enable_if<_Test,_Ty>::type loadData(V1<V2<double>> &,std::string)': could not deduce template argument for 'V1<V2<double>> &' from 'std::vector<std::vector<double,std::allocator<_Ty>>,std::allocator<std::vector<_Ty,std::allocator<_Ty>>>>'
with
[
_Ty=void
]
and
[
_Ty=double
]
<source>(22): note: see declaration of 'loadData'
<source>(51): error C2782: 'enable_if<_Test,_Ty>::type loadData(V1<V2<double>> &,std::string)': template parameter 'V2' is ambiguous
with
[
_Ty=void
]
<source>(22): note: see declaration of 'loadData'
<source>(51): note: could be 'std::_Vector_alloc'
<source>(51): note: or 'std::vector'
Это VS или ошибка кода? Любые предложения о том, как это исправить?
Я попытался скомпилировать с /permissive-
и /std:latest
, ничего не изменилось.
Оригинальный код был предложен в принятом ответе здесь:
Шаблон функции, который принимает как std :: vector, так и QVector?
Ссылка проводника компилятора: https://godbolt.org/g/by7nBM
Спасибо!