【发布时间】: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