【问题标题】:Visual Studio 2017 - could not deduce template argument (with variadic templates)Visual Studio 2017 - 无法推断模板参数(使用可变参数模板)
【发布时间】:2018-07-02 11:16:46
【问题描述】:

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

谢谢!

【问题讨论】:

  • 更短的例子:godbolt.org/g/U28P9R
  • @aschepler 是的,看起来 MSVC 不喜欢其模板模板参数中的默认模板参数...

标签: c++ visual-studio-2017 variadic-templates c++17


【解决方案1】:

这是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>
// .....................................^^^^

【讨论】:

  • 这通常不是问题。编译器可以在内部将它们区分开来,但是当它在错误消息中将它们打印出来时,它们恰好具有相同的名称。
  • 仍然是同样的错误,不幸的是(用编译器资源管理器检查)。看起来也像编译器错误(不仅仅是 stdlib)。
【解决方案2】:

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

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,我认为这是必要的。也许不再那么多了。

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

但是...现在我遇到了这个编译器错误。

知道是否有patch coming in response

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 2013-02-18
    • 1970-01-01
    • 2017-03-22
    • 2020-07-01
    • 2013-09-14
    相关资源
    最近更新 更多