gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:46:51 25 4
gpt4 key购买 nike

以下代码在 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 进行编译,但没有任何区别。

此处接受的答案中建议了原始代码: A function template that accepts both std::vector and QVector?

编译器资源管理器链接:https://godbolt.org/g/by7nBM

谢谢!

最佳答案

Is this a VS or a code bug?

我打赌“D'oh!”那是一个 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>
// .....................................^^^^

关于c++ - Visual Studio 2017 - 无法推断模板参数(使用可变参数模板),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51134948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com