问题
The following code compiles and works fine in gcc and clang, but fails to compile in 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;
}
This is the error message:
<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'
Is this a VS or a code bug? Any suggestions on how to fix it?
I did try compiling with /permissive-
and /std:latest
, didn't make a difference.
The original code was suggested in the accepted answer here: A function template that accepts both std::vector and QVector?
Compiler explorer link: https://godbolt.org/g/by7nBM
Thanks!
回答1:
Is this a VS or a code bug?
I bet a "D'oh!" that is a VS bug.
See the second message error
<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
]
The same _Ty
identifier is used for two differents defaults template parameters:
One
enable_if<_Test,_Ty>::type
// .............^^^ (void)
Two
std::vector<double,std::allocator<_Ty>>`
// ...............................^^^ (double)
They should be different identifiers.
Suggestion: try explicating the first one
-> std::enable_if_t<isVector<V>::value, void>
// .....................................^^^^
回答2:
I too have bumped into this nasty little bug. I think it's a bug as well. The following code used to work under previous versions:
namespace Catch {
template<typename Ty_, typename... Args_>
std::string Tags(Ty_ x, Args_... others) {
return Tags(x) + Tags(others...);
}
}
And so on, and would be useful as follows:
TEST_CASE("my test case", Catch::Tags("one", "two", "fred", "red")) {
// ...
}
With Catch2, not sure if you need to append an .c_str()
on that; at least in Catch
, I think that was necessary. Perhaps not so much any more.
I did this in an effort to keep from having to trip up over tag syntax, and I could simply focus on the tags themselves and leave the boilerplate syntax for the variadic functions.
However... Now I've run afoul of this compiler bug.
Any idea if there is a patch coming in response?
来源:https://stackoverflow.com/questions/51134948/visual-studio-2017-could-not-deduce-template-argument-with-variadic-templates