本文介绍了何时使用详细说明的类型说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有特别好的理由选择使用精心设计的类型说明符?例如,在某些情况下,需要使用模板 typename 关键字来消除依赖 template 或键入。

Is there a particularly good reason to choose to use an elaborated type specifier? For example, in certain circumstances, one is required to use the template or typename keywords to disambiguate a dependent template or type.

但我不能想到任何例子,例如枚举等。以下代码示例:

But I can't think of any examples where this would occur for something such as an enumeration. Take the following code example:

enum Foo { A,  B };

void bar(Foo foo);
void baz(enum Foo foo);

为什么我可以选择使用语法 baz()提供 bar()(反之亦然)?有什么不明确的情况吗?

Why might I choose to use the syntax baz() provides over bar() (or vice-versa)? Is there any ambiguous case?

推荐答案

没有理由使用这样的说明符,除非你处理的情况下,不同种类。例如,在枚举声明之后声明一个名为 Foo 的变量是完全合法的,因为非正式地说,对象名和类型名存在于独立的命名空间中(见3.3 / 4为更正式的规范)

There are no reasons to use such specifiers, unless you are dealing with the situation when the name is hidden by name of a different "kind". For example, it is perfectly legal to declare a variable named Foo after the enum declaration, since, speaking informally, object names and type names live in independent "namespaces" (see 3.3/4 for more formal specification)

enum Foo { A, B };

int Foo;

int Foo 声明后, code> bar 声明将无效,而更精细的 baz 声明将保持有效。

After the int Foo declaration, your bar declaration will become invalid, while the more elaborate baz declaration will remain valid.

这篇关于何时使用详细说明的类型说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 14:32