使用声明是否等同于使用声明的终结符名称等于标识符的别名声明?

huangapple go评论60阅读模式
英文:

Is a using declaration for a non-member type equivalent to an alias declaration with an identifier equal to the terminal name of the using declarator?

问题

以下是翻译好的部分:

In short, I'm asking if doing

using foo::bar::baz;

has the same effect as

using baz = foo::bar::baz;

(Clearly I'm assuming that foo::bar::baz names a type that is not a class member, e.g. I'm referring to something like namespace foo::bar { using baz = int; }, and not to something like namespace foo { struct bar { using baz = int; }; }.)
I'm pretty sure they are two different things (otherwise they could also always stay side by side, which is not the case, as I know that the former can't be in a struct/class, unlike the latter), but how do I read it from the standard? I was looking at [namespace.udecl] and [dcl.pre], but I can't really draw a conclusion.

英文:

In short, I'm asking if doing

using foo::bar::baz;

has the same effect as

using baz = foo::bar::baz;

(Clearly I'm assuming that foo::bar::baz names a type that is not a class member, e.g. I'm referring to something like namesapce foo::bar { using baz = int; }, and not to something like namespace foo { struct bar { using baz = int; }; }.)

I'm pretty sure they are two different things (otherwise they could also always stay side by side, which is not the case, as I know that the former can't be in a struct/class, unlike the latter), but how do I read it from the standard? I was looking at [namespace.udecl] and [dcl.pre], but I can't really draw a conclusion.

答案1

得分: 1

代码部分不翻译,只返回翻译好的内容:

There are definitely differences between the two, but most of them are of no interest to most programmers. For example,
一个明显的区别是,两者之间存在差异,但其中大多数对大多数程序员来说都不感兴趣。例如,

One practical distinction is in exporting a previously declared name with module linkage:
一个实际的区别在于,导出以前声明的具有模块链接的名称:

The using-declaration can of course also be applied to non-type declarations, whereas the alias-declaration can also be a template.
using-declaration 当然也可以应用于非类型声明,而alias-declaration 也可以是一个模板。

英文:

There are definitely differences between the two, but most of them are of no interest to most programmers. For example,

namespace N {
  struct X {};
  struct Y {};
  struct Z {};
}
namespace O {
  int X,Y;
  using N::X;    // OK
  struct X x;    // OK, finds N::X
  int i=X;       // OK, finds O::X
  using Y=N::Y;  // error: conflicts with “int Y”
  using Z=N::Z;
  struct Z z;    // error: elaborated-type-specifier with typedef-name
}

One practical distinction is in exporting a previously declared name with module linkage:

module A:S;
struct S {};
export module A;
import :S;
export using ::S;  // error: “struct S” has module linkage
export using S=S;  // OK

The using-declaration can of course also be applied to non-type declarations, whereas the alias-declaration can also be a template.

huangapple
  • 本文由 发表于 2023年2月7日 04:36:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定