Visual Studio 2017 - 无法推断模板参数(使用可变参数模板)

Visual Studio 2017 - could not deduce template argument (with variadic templates)

本文关键字:参数 变参 Studio 2017 Visual      更新时间:2023-10-16

以下代码在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

谢谢!

这是一个VS还是一个代码错误?

我敢打赌"哦!"这是一个VS错误。

请参阅第二条消息错误

<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
]

相同的_Ty标识符用于两个不同的默认模板参数:

enable_if<_Test,_Ty>::type
// .............^^^ (void)

std::vector<double,std::allocator<_Ty>>`
// ...............................^^^ (double)

它们应该是不同的标识符。

建议:尝试解释第一个

-> std::enable_if_t<isVector<V>::value, void>
// .....................................^^^^

我也遇到了这个讨厌的小虫子。我认为这也是一个错误。以下代码用于在以前的版本下工作:

namespace Catch {
template<typename Ty_, typename... Args_>
std::string Tags(Ty_ x, Args_... others) {
return Tags(x) + Tags(others...);
}
}

依此类推,将按如下方式有用:

TEST_CASE("my test case", Catch::Tags("one", "two", "fred", "red")) {
// ...
}

对于Catch2,不确定是否需要在此附加.c_str();至少在Catch中,我认为这是必要的。也许不再那么多了。

我这样做是为了避免被标签语法绊倒,我可以简单地关注标签本身,而将样板语法留给可变参数函数。

然而。。。现在我已经遇到了这个编译器错误。

知道是否有补丁作为响应吗?