1
以下代码当VC产生编译错误++ 2010:VC++函数模板实例错误C2664使用不同的枚举参数
// cpptests.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
template <typename U1>
int insert_unit01(const U1& u1) {
return 0;
}
enum {A1,A2 };
enum {B1,B2 };
int _tmain(int argc, _TCHAR* argv[])
{
cout << insert_unit01(A1) << endl;
cout << insert_unit01(B1) << endl;
return 0;
}
C:\work\cpptests.cpp(23): error C2664: 'insert_unit01' : cannot convert
parameter 1 from '' to 'const &' Reason: cannot convert from ''
to 'const 'Conversion to enumeration type requires an explicit cast
(static_cast, C-style cast or function-style cast)
的问题是,我用另一个枚举B1
(后实例相同的模板功能A1
)。
我是新来的C++模板。为什么会有这样的错误?
做论证推断时是否有与enum
有关的具体规则?它是特定于VC++ 2010吗?
编辑:请注意,这个编译正确(不调用模板函数使用B1
第二次)
int _tmain(int argc, _TCHAR* argv[])
{
cout << insert_unit01(A1) << endl;
return 0;
}
那么,为什么它没有实例化一个第二功能insert_unit01(B1)
,而是产生一个编译错误?
这是否意味着VC++第一次调用'insert_unit01(A1)'时不会产生错误? – JavaMan
@JavaMan:“错”是一个大词。严格遵守标准是很少见的 - 大多数实现都会有自己的扩展,或稍微偏离行为。 –
即使行为偏离标准,错误也是适合的。由于第一个函数调用'insert_unit01(A1)'可能会隐藏在另一个间接包含多层嵌套#includes的头文件中(这正是我所面对的)。而第二个函数调用会在远程src文件中神秘地导致错误,而没有任何提示可以追溯到第一个函数调用。 – JavaMan