加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 没有匹配的函数调用

发布时间:2020-12-16 07:51:50 所属栏目:百科 来源:网络整理
导读:鉴于: templatetypename Tvoid f( T ) {}enum { // if changed to "enum E" it compiles e};int main() { f( e ); // line 10} 我得到: foo.cpp: In function ‘int main()’:foo.cpp:10: error: no matching function for call to ‘f(anonymous enum)’
鉴于:
template<typename T>
void f( T ) {
}

enum {    // if changed to "enum E" it compiles
  e
};

int main() {
  f( e ); // line 10
}

我得到:

foo.cpp: In function ‘int main()’:
foo.cpp:10: error: no matching function for call to ‘f(<anonymous enum>)’

然而,如果枚举声明被赋予一个名称,那么它将被编译.为什么它不适用于匿名枚举?理想情况下,我希望将枚举值e提升为int并实例化f(int).

解决方法

未命名的类型根本不能用作模板参数

C 03在14.3.1 [temp.arg.type] / 2中说

A local type,a type with no linkage,an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.

此限制在C 0x中解除,您的程序在C 0x模式下不使用MSVC 2010和gcc 4.5.2进行编译.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读