为什么没有范围的枚举的声明会编译? [英] Why unscoped enums' declaration compiles?

查看:191
本文介绍了为什么没有范围的枚举的声明会编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Scott Meyers的 Effective Modern C ++ 书中,提到了无范围的枚举和有范围的枚举(枚举类)之间的主要区别之一是我们无法转发声明前者(请参阅第3章第10项-将作用域枚举优先于无作用域的枚举 )。例如:

 枚举颜色; * //错误!* 
枚举类Color; * //罚款*

但是我在下面写了一个小的例子,但事实并非如此。



test.h

  #pragma一次
枚举名称;
void print(names n);

test.cpp

  #include test.h 

#include< iostream>

枚举名称{John,Bob,David};

void print(names n)
{
switch(n)
{
case John:
case Bob:
案例David:
std :: cout<< 名字。 << std :: endl;
休息时间;
默认值:
std :: cout<< 其他事物<< std :: endl;
};
}

main.cpp

  #include test.h 
#include< iostream>

int main()
{
名称n = static_cast< names>(2);
print(n);
返回0;
}

我使用了VC14编译器(Visual Studio 2015)。这是编译器的错误或功能吗?还有吗如果这是错误或功能(考虑到Meyers认为这是无作用域的和有作用域的枚举之间的主要区别),则意味着编译器能够处理我们可以声明无作用域的枚举的情况。因此,是否需要在枚举之后添加新的关键字 class ?当然,有人会说范围枚举还有其他两个功能。就我所知,上述功能是最重要的。

解决方案

C ++ 11


首先:C ++ 11允许向前声明无作用域的枚举。您只需将基础类型提供给编译器(例如,来自Effective Modern C ++,第3章,第10项):

 枚举颜色:std: :uint8_t; 

您使用的是编译器的非标准扩展,但仍然可能。


重要与否


Scott Meyers在引用的章节中说:


似乎范围内的枚举与未限制范围的
枚举相比,它具有第三个优势,因为可以预先声明作用域枚举,即,可以在不指定其枚举数的情况下声明其名称

[...]在$ b $中b C ++ 11,也可以向前声明无作用域的枚举,但是仅在
的额外工作之后才可以声明。


似乎意思是是的反义词。斯科特·迈耶斯(Scott Meyers)自己说,这不是所说书中的重要功能。

In the Effective Modern C++ book of Scott Meyers it is mentioned that one of the main difference between unscoped and scoped enums(enum class) is that we can't forward declare the former (see Chapter 3, Item 10 - "Prefer scoped enums to unscoped enums"). For example:

enum Color;            *// error!*
enum class Color;      *// fine*

But I've written below mentioned small example and saw that it is not so.

test.h

#pragma once
enum names;
void print(names n);

test.cpp

#include "test.h"

#include <iostream>

enum names { John, Bob, David };

void print(names n)
{
    switch (n)
    {
    case John:
    case Bob:
    case David:
        std::cout << "First names." << std::endl;
        break;
    default:
        std::cout << "Other things" << std::endl;
    };
}

main.cpp

#include "test.h"
#include <iostream>

int main()
{
    names n = static_cast<names>(2);
    print(n);
    return 0;
}

I've used VC14 compiler (Visual Studio 2015). Is this bug or feature of the compiler? Something else? If this is bug or feature (considering that Meyers says that this is the major difference between unscoped and scoped enums) it means that compilers are capable of managing the situation where we can declare unscoped enums. Hence, was there a need to add a new keyword class after enum? Of course, someone will say that there are other two features of scoped enums. As I got it, above mentioned feature is the most important.

解决方案

C++11

First: C++11 does allow forward declaration of unscoped enums. You just have to give the underlying type to the compiler (example from Effective Modern C++, Chapter 3, Item 10):

enum Color: std::uint8_t;

What you use is a non-standard extension of the compiler, but its possible anyway.

important or not

Scott Meyers says in the quoted chapter:

It may seem that scoped enums have a third advantage over unscoped enums, because scoped enums may be forward-declared, i.e., their names may be declared without specifying their enumerators: [...] In C++11, unscoped enums may also be forward-declared, but only after a bit of additional work.

"May seem" means the opposite of "is". Scott Meyers himself states that this is not an important feature in said book.

这篇关于为什么没有范围的枚举的声明会编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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