为什么有一个注入的类名? [英] Why is there an injected class name?

查看:145
本文介绍了为什么有一个注入的类名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我看到了一个奇怪的C ++特性:注入的类名

  }; 
X x1;
class X :: X x2; // class X :: X等于X
class X :: X :: X x3; // ...等等...

但我不知道为什么这个功能是必要的。是否有任何需要此功能的做法?



我听说这个功能在旧的C ++中不存在。然后,什么时候介绍? C ++ 03? C ++ 11?

解决方案

注入的类名意味着 X 被声明为 X 的成员,因此 X 中的名称查找总是查找当前类,而不是另一个<$例如

  void X(c $ c) ){} 
class X {
public:
static X create(){return X(); }
};

create()临时 X 对象或调用函数 X ?在命名空间范围它将调用该函数,因此inject-class-name的目的是确保在 X 的正文中,该名称始终找到类本身名称查找在类的自己的范围内开始,然后再查找包含的范围)。



在类模板中也有用,注入的类名可以在没有模板的情况下使用参数列表,例如使用简单的 Foo ,而不是完整的模板id Foo 到当前实例化。有关C ++ 98和C ++ 03之间的更改,请参见 DR 176 。澄清了这一点。



注入的类名的想法出现在C ++ 98中,但是C ++ 03的术语是新的。



C ++ 98说:


插入到在看到 class-name 之后立即声明的范围中。


第二句话改为 DR 147 ,因此C ++ 03在[class] / 2中说:


一个 class-name 被插入到紧接在 class-name 之后声明的范围看到。 class-name 也被插入类本身的范围;这被称为 inject-class-name


即使在C ++ 98之前,ARM具有大致相同的措辞,意味着类的名称总是可以在类体中用于引用类本身:


类可以用作类说明符本身的 member-list 中的类名



  • 例如,


    class link {link * next; };




/ blockquote>

Recently, I saw a strange C++ feature: injected class name.

class X { };
X x1;
class X::X x2; // class X::X is equal to X
class X::X::X x3; // ...and so on...

But I cannot figure out why this feature is necessary. Is there any practice that requires this feature?

And I heard this feature didn't exist in old C++. Then, when was it introduced? C++03? C++11?

解决方案

The injected class name means that X is declared as a member of X, so that name lookup inside X always finds the current class, not another X that might be declared at the same enclosing scope, e.g.

void X() { }
class X {
public:
  static X create() { return X(); }
};

Is the create() function creating a temporary X object or calling the function X? At namespace scope it would call the function, so the purpose of the injected-class-name is to ensure that within the body of X the name always finds the class itself (because name lookup starts in the class' own scope before looking in the enclosing scope).

It's also helpful inside class templates, where the injected class name can be used without a template argument list, e.g. using simply Foo instead of the full template-id Foo<blah, blah, blah>, so it's easy to refer to the current instantiation. See DR 176 for a change between C++98 and C++03 that clarified that.

The idea of the injected class name was present in C++98, but the terminology was new for C++03.

C++98 says:

A class-name is inserted into the scope in which it is declared immediately after the class-name is seen. The class-name is also inserted into the scope of the class itself.

The second sentence was changed by DR 147 so C++03 says in [class]/2:

A class-name is inserted into the scope in which it is declared immediately after the class-name is seen. The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name.

Even before C++98, the ARM has roughly equivalent wording that means the class' name can always be used in the class body to refer to the class itself:

The name of a class can be used as a class-name even within the member-list of the class specifier itself.

  • For example,

    class link { link* next; };

这篇关于为什么有一个注入的类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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