【问题标题】:MSVC compiler error when overloading template with enum parameter使用枚举参数重载模板时 MSVC 编译器错误
【发布时间】:2019-02-18 21:41:28
【问题描述】:

我尝试编写将枚举类作为非类型模板参数的模板,如下面的代码所示。

当我尝试使用 MSVC2017 编译此代码时,我收到以下编译器错误:

source_file.cpp(16): error C2668: 'g': ambiguous call to overloaded function
source_file.cpp(10): note: could be 'void g<E2::v>(void)'
source_file.cpp(6): note: or       'void g<E1::v>(void)'
source_file.cpp(16): note: while trying to match the argument list '()'

另一方面,Clang 和 gcc 编译我的代码没有任何错误消息和输出

g1
g2

正如预期的那样。

#include <iostream>

enum class E1{v};
enum class E2{v};

template<E1 e1> void g(){
    std::cout << "g1" << std::endl;
}

template<E2 e2> void g(){
    std::cout << "g2" << std::endl;
}

int main(){
    g<E1::v>();
    g<E2::v>();
}

(此代码可以在rextester上测试。)

我的代码有错误还是 MSVC 有问题?您知道解决此问题的任何方法吗?

【问题讨论】:

    标签: c++ templates visual-c++ enums


    【解决方案1】:

    我的代码有错误还是 MSVC 有问题?

    对我来说,这看起来像是 MSVC 中的一个错误。

    您知道解决此问题的任何方法吗?

    似乎如果E1::vE2::v 有不同的值,那么它编译正确。我通过将您的第二个枚举更改为此进行了测试:

    enum class E2 { v = 2 };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 2011-12-17
      相关资源
      最近更新 更多