A“使用” 带有枚举的声明
using 声明似乎不适用于枚举类型:
class Sample{
public:
enum Colour {RED, BLUE, GREEN};
}
using Sample::Colour;
不起作用!
我们是否需要为每个枚举类型的枚举器添加一个 using 声明? 就像下面这样:
using sample::Colour::RED;
A using declaration does not seem to work with an enum type:
class Sample{
public:
enum Colour {RED, BLUE, GREEN};
}
using Sample::Colour;
does not work!
Do we need to add a using declaration for every enumerators of enum type? Like below:
using sample::Colour::RED;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要添加到 Steve Lacey 的答案,原始代码的问题是您引用一个成员,但 using 声明本身并不是一个成员声明:
7.3.3/6 有:
为了强调这一点,以下示例确实有效:
最后,正如 Igor Semenov 指出的那样: ,即使您将枚举定义移动到命名空间中,从而允许 using 声明,using 声明也只会将枚举类型的名称声明到命名空间中(2003标准参考为 7.3.3/2)。
依赖基类型
当编译器解析类模板时,允许部分和显式特化。 它不会在依赖基类中执行任何查找。 因此,以 Sample 作为模板的以下变体无法编译:
问题是
Derived::Colour
被编译器视为对象 (14.6/2):查看名称成为类型的两个条件:
Colour
未找到类型,因为未搜索依赖的基Sample
。typename
限定,因此该示例需要
typename
关键字:注意: 1998 版本的标准不允许
typename
与 using 声明一起使用,因此上述修复是不可能的。 请参阅从依赖基类访问类型和CWG11。To add to Steve Lacey's answer, the problem with the original code is that you refer to a member, but the using declaration is not itself a member declaration:
7.3.3/6 has:
To highlight this, the following example does work:
Finally, as pointed out by Igor Semenov, even if you move the enum definition into a namespace, thereby allowing the using declaration, the using declaration will only declare the name of the enum type into the namespace (the 2003 standard reference is 7.3.3/2).
Dependent Base Types
To allow for partial and explicit specializations, when the compiler parses a class template. it does not perform any lookups in dependent base classes. As a result, the following variation with Sample as a template does not compile:
The problem is that
Derived::Colour
is treated as an object by the compiler (14.6/2):Looking at the two conditions for the name to be a type:
Colour
doesn't find a type because the dependent baseSample<T>
is not searched.typename
The example therefore needs the
typename
keyword:Note: The 1998 version of the standard didn't allow
typename
to be used with a using declaration and so the above fix was not possible. See Accessing types from dependent base classes and CWG11.类没有定义名称空间,因此“使用”在这里不适用。
另外,您需要公开枚举。
如果您尝试在同一个类中使用枚举,请参阅以下示例:
并从类外部访问它:
A class does not define a namespace, and therefore "using" isn't applicable here.
Also, you need to make the enum public.
If you're trying to use the enum within the same class, here's an example:
And to access it from outside the class:
C++ 标准,7.3.3.1:
C++ Standard, 7.3.3.1:
现在,有一个相关的问题: 'using enum' in C++ 20。
看起来 C++20 将可以选择使用
using enum
声明,从而最终允许直接访问枚举类的成员,如下所示 (source):当然,这意味着在
S
内部,您也可以简单地使用orange
和apple
而不是fruit::orange
和fruit::apple
。By now, there's a related question: 'using enum' in C++20.
It looks like C++20 will have the option make a
using enum
declaration, thus finally allowing direct access to an enum class' members, like this (source):Of course, that means that inside
S
, you will also be able to simply useorange
andapple
instead offruit::orange
andfruit::apple
.