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

c – 为什么“case :: LABEL:”用g编译?

发布时间:2020-12-16 10:01:02 所属栏目:百科 来源:网络整理
导读:参见英文答案 Scope resolution operator and const????????????????????????????????????4个 我很惊讶地得知g(4.9)正在编译这个(而gcc不会): #include stdio.henum{ ONE = 1,TWO = 2,THREE = 3};int main(int argc,char* argv[]){ int sw = 2; switch (sw)
参见英文答案 > Scope resolution operator and const????????????????????????????????????4个
我很惊讶地得知g(4.9)正在编译这个(而gcc不会):

#include <stdio.h>

enum
{
    ONE   = 1,TWO   = 2,THREE = 3
};

int main(int argc,char* argv[])
{
    int sw = 2;

    switch (sw)
    {
    case::ONE:
    {
        printf("1n");
        break;
    }

    case::TWO:
    {
        printf("2n");
        break;
    }

    case::THREE:
    {
        printf("3n");
        break;
    }

    default:
    {
        printf("defaultn");
    }
    }
}

g预处理器如何能够将“case”与“:: ONE:”分开?

解决方法

How is the g++ preprocessor able to separate the “case” from the “::ONE:”?

它不是预处理器,而是简单的编译器,它解释::引用全局命名空间.

案例标签将被解析为

case :: THREE :

这完全没问题,因为你的枚举值出现在全局命名空间(::)中.

(编辑:李大同)

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

    推荐文章
      热点阅读