函数内定义的类型可以实例化模板吗?

luuillu 2011-08-31 08:11:26
下面的程序在vs2010下可以正常通过编译,但在Dev-C++下却不能通过编译。为什么会出现这种情况? 谢谢。


teplate<typename T>
void f(const T& t)
{

}

void g()
{
enum Week{Mon, Tue, Wed};
Week day = Mon;
f(day);
}

int main()
{}

...全文
151 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
2011-09-01
  • 打赏
  • 举报
回复
C++03:
14.3.1/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 里面这个规定被拿掉了。
luuillu 2011-09-01
  • 打赏
  • 举报
回复
修改一下

int x[99];
void f()
{
struct x { int a; };
sizeof(x); /* size of the array in C */
/* size of the struct in C + + */
}


luuillu 2011-09-01
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 cangyingzhijia 的回复:]
enum Week{Mon, Tue, Wed};
不能放在函数里定义,vs2010能通过编译是ms自己vc的扩展功能,非标准c++用法
[/Quote]
函数里面可以定义类型,可见c++2003标准702页。
C.1.7 Clause 9: classes [diff.class]
9.1 [see also 7.1.3]
Change: In C + +, a class declaration introduces the class name into the scope where it is declared and hides any object, function or other declaration of that name in an enclosing scope. In C, an inner scope declaration of a struct tag name never hides the name of an object or function in an outer scope
Example:

void f()
{
struct x { int a; };
sizeof(x); /* size of the array in C */
/* size of the struct in C + + */
}


int x[99];
Rationale: This is one of the few incompatibilities between C and C + + that can be attributed to the new C + + name space definition where a name can be declared as a type and as a nontype in a single scope causing the nontype name to hide the type name and requiring that the keywords class, struct, union or enum be used to refer to the type name. This new name space definition provides important notational conveniences to C + + programmers and helps making the use of the user-defined types as similar as possible to the use of built-in types. The advantages of the new name space definition were judged to outweigh by far the incompatibility with C described above.
苍蝇①号 2011-08-31
  • 打赏
  • 举报
回复
enum Week{Mon, Tue, Wed};
不能放在函数里定义,vs2010能通过编译是ms自己vc的扩展功能,非标准c++用法
gykgod 2011-08-31
  • 打赏
  • 举报
回复
深刻 学习了~
2011-08-31
  • 打赏
  • 举报
回复
晕,7 楼被吞了……
FrankHB1989 2011-08-31
  • 打赏
  • 举报
回复
记得C++03不行,C++0x可以。
2011-08-31
  • 打赏
  • 举报
回复
局部类型不能用于实例化模板,语法上就这么规定的,虽然这个规定很扯……
pathuang68 2011-08-31
  • 打赏
  • 举报
回复
别这么干就行了
taodm 2011-08-31
  • 打赏
  • 举报
回复
C++98/2003规定:不可以。
xaojin2011 2011-08-31
  • 打赏
  • 举报
回复
语法没有问题啊 ,
jackyjkchen 2011-08-31
  • 打赏
  • 举报
回复
放到外面就可以……不知gcc为何有此限制

template<typename T>
void f(const T& t)
{

}
enum Week{Mon, Tue, Wed};
void g()
{
Week day = Mon;
f(day);
}

int main()
{}

jackyjkchen 2011-08-31
  • 打赏
  • 举报
回复
gcc 4.4.5也不行
jackyjkchen 2011-08-31
  • 打赏
  • 举报
回复
dev c++是gcc 3.4吧,6年前的版本了……

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧