在类声明中的全局限定class-head [英] Global qualification in a class declarations class-head

查看:407
本文介绍了在类声明中的全局限定class-head的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们发现了类似以下的东西(不要问...):

 命名空间N {
struct A {struct B; };
}

struct A {struct B; };

使用命名空间N;

struct :: A :: B {}; // < - 兴趣点

有趣的是,这与VS2005,icc 11.1和Comeau在线),但无法通过GCC:


类名称的全局限定在{'token


从C ++ 03,附件A,在我看来像GCC是正确的:




  • class-head 可以包含嵌套名称说明符标识符

  • 嵌套名称说明符不能以全局限定符开头显然,标识符



  • ...或我可以俯瞰某物吗?

    解决方案

    你是正确的:GCC在这种情况下实现标准的字母,而其他实施它不那么严格(看看 issue#355 )。



    您可以执行下列操作来解决语法的限制

      struct identity< :: A> :: type :: B {}; 

    或者使用显式名称 typedef / p>

      typedef :: A AHidden; 
    struct AHidden :: B {};

    或者,当然,您可以使用命名空间交换的顺序和嵌套类定义。请注意,附件A仅供参考。规范性文本在 5.1 / 7 9 条款。


    We found something similar to the following (don't ask ...):

    namespace N {
        struct A { struct B; };
    }
    
    struct A { struct B; };
    
    using namespace N;
    
    struct ::A::B {}; // <- point of interest
    

    Interestingly, this compiles fine with VS2005, icc 11.1 and Comeau (online), but fails with GCC:

    global qualification of class name is invalid before '{' token

    From C++03, Annex A, it seems to me like GCC is right:

    • the class-head can consist of nested-name-specifier and identifier
    • nested-name-specifier can't begin with a global qualification (::)
    • obviously, neither can identifier

    ... or am i overlooking something?

    解决方案

    I think you are getting it right: GCC implements the standard to the letter in this case, while the others implement it less strict (have a look at issue #355).

    You could do the following to work-around the limitation of the syntax

    struct identity< ::A >::type::B {}; 
    

    Or you use an explicit named typedef

    typedef ::A AHidden;
    struct AHidden::B { };
    

    Or, of course, you exchange the order of using namespace and the nested class definition. Notice that Annex A is informative only. The normative text is at clauses 5.1/7 and 9.

    这篇关于在类声明中的全局限定class-head的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆