Visual Studio 2017 - 无法推断模板参数(使用可变参数模板)
Visual Studio 2017 - could not deduce template argument (with variadic templates)
以下代码在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
中,我认为这是必要的。也许不再那么多了。
我这样做是为了避免被标签语法绊倒,我可以简单地关注标签本身,而将样板语法留给可变参数函数。
然而。。。现在我已经遇到了这个编译器错误。
知道是否有补丁作为响应吗?
相关文章:
- 在不传递参数数量且只有3个点的情况下,如何使用变差函数
- 如何使用可变参数模板强制转换每个变体类型
- 关于如何在具有单个参数的变体构造中选择替代方案?
- 调用参数排列不变函数 f(i++, i++)
- 参数归纳与标准::变体
- 模板化回调参数的逆变,如 C# 中的逆变
- 如何在没有参数包的情况下编写变差函数
- 通过具有嵌套类的工厂类获取多个变异类模板参数包
- 获取模板参数的成员变量值列表
- 保留短 lambda 用作函数的中间参数,使用 clang 格式保持不变
- 如何定义变体<x,y,z>提取模板参数的子类型
- 正确对齐内存模板,参数顺序不变
- 递归中不同参数类型的变元模板函数
- 通过函数指针传递给变差函数的参数会更改其值
- 提升预定义为带有参数的全局 lambda 的变体访问者
- 使用可变参数模板参数提升变体访问者
- boost ::变体 - 为什么模板参数比const字符串参数具有更高的优先级
- 将变参数包中的值加载到临时数组中
- 使用额外参数提升变体访客
- 正在将动态数组元素解析为参数?(变音符)