你如何理解C ++中的依赖名称 [英] How do you understand dependent names in C++

查看:150
本文介绍了你如何理解C ++中的依赖名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这个术语依赖名称通常在模板的上下文中。然而,我很少碰到后者。因此,自然想知道更多关于依赖名称的概念。

I come across this term "dependent names" typically in the context of templates. However, I rarely touch the latter. Thus naturally would like to know more about the concept of dependent names.

在模板的上下文中你如何理解它?示例被极度鼓励!

How do you understand it in the context of templates and outside of them? example are critically encouraged!

推荐答案

依赖名称本质上是一个依赖于模板参数的名称。

A dependent name is essentially a name that depends on a template argument.

当使用模板时,模板的定义点和实例化点之间有一个区别,即您实际使用模板的位置。依赖于模板的名字在实例化之前不会绑定,而在定义点不绑定的名称。

When using templates there is a distinction between the point of definition of the template and the point of instantiation i.e. where you actually use the template. Names that depend on a template don't get bound until the point of instantiation whereas names that don't get bound at the point of definition.

一个简单的例子是:

template< class T > int addInt( T x )
{
    return i + x.toInt();
}

其中声明或定义 i 将需要在之前出现,因为 i 不依赖于模板参数 T ,因此在定义点上是约束的。未知类型 x 变量的 toInt 成员的定义只能出现在 addInt 函数实际上在某处使用,因为它是一个依赖名称(技术上,实例化点被视为在使用点之前最近的全局或命名空间范围,因此它具有在此之前可用)。

where a declaration or definition of i would need to appear before the definition given above since i does not depend on the template argument T and is therefore bound at the point of definition. The definition of the toInt member of the as-yet-unknown-type x variable only has to appear before the addInt function is actually used somewhere as it is a dependent name ( technically the point of instantiation is taken as the nearest enclosing global or namespace scope just before the point of use and so it has to be available before that ).

这篇关于你如何理解C ++中的依赖名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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