Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1066576
  • 博文数量: 77
  • 博客积分: 11498
  • 博客等级: 上将
  • 技术积分: 1840
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-04 11:10
文章分类

全部博文(77)

文章存档

2011年(1)

2010年(16)

2009年(5)

2008年(55)

分类: C/C++

2008-11-23 22:28:02

在标准C++03(C++98)中,模板实例化时不能有连续的右尖括号,因为这会被解析为>>操作符,因此必须在模板实例时用空格来分隔连续的右尖括号。显然,这减轻了编译器的负担,却给程序员增加了负担。而语言的发展趋势是编译器尽可能做更多的工作,而把程序员解放出来去做真正需要做的事情。

于是,在标准C++0x中,这个限制得到了解除,模板实例化时允许出现连续的右尖括号了。但如果在模板实例化的尖括号内有>运算符(>和>>两个运算符)参与的表达式,则必须用圆括号把该表达式括起来。

F:\tmp>gcc --version
gcc (GCC) 4.3.0 20080305 (alpha-testing) mingw-20080502
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

F:\tmp>type right_angle_brackets.cpp
template
class X {};

template
class Y {};

int main()
{
    X< 1>2 > x1;      // error
    X<(1>2)> x2;      // ok

    Y> y1;       // ok
    Y>1>> y2;    // error
    Y>1)>> y3;  // ok
}

F:\tmp>g++ -std=c++0x right_angle_brackets.cpp
right_angle_brackets.cpp: In function 'int main()':
right_angle_brackets.cpp:9: error: expected unqualified-id before numeric consta
nt
right_angle_brackets.cpp:13: error: expected unqualified-id before numeric const
ant

F:\tmp>

关于此问题的解决方法和更有趣的信息请参考
阅读(2232) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~