它是关于模板的依赖名称解析的标准中的缺陷吗? [英] Is it a defect in the standard about dependent name resolution for template

查看:62
本文介绍了它是关于模板的依赖名称解析的标准中的缺陷吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何查找模板的依赖名称,标准仅给出了这样的一句话,没有更多了:

About how lookup the dependent name for template, The standard only gives a little sentence like this, there's no more:


依赖名称,请考虑以下来源的名称:

In resolving dependent names, names from the following sources are considered:



  1. 点可见的声明模板的定义

  2. 来自实例化上下文([temp.point])和定义上下文中与函数参数类型关联的名称空间的声明。



请考虑以下代码

struct Test{
  using type = int;
};
// #1
template<typename T>
struct TMP{
  using type = typename T::type;
};
int main(){
  TMP<Test>::type v = 0;
}

对于此代码,名称为 type 确实是一个从属名称,因为T是模板参数,而不是函数调用,因此,唯一相关的项目符号是数字1。它仅表示从属名称在模板定义,这意味着在我的代码中,该声明应在#1 处可见。实际上, typename T :: type 是一个限定ID,因此限定名称查找规则适用于它,因为 T 是模板参数,因此查找动作应在给定模板参数之后发生,即在实例化此类模板的过程中。但是我引用的话并没有说什么。因此,我想知道这是标准的缺陷吗?如果我错过了任何能解释标准的内容,请为这个问题引用它们。

For this code, the name type indeed a dependent name because T is a template parameter and here is not a function call, So, the only relevant bullet point is Number 1. It only says the dependent name shall be visible before the template definition, It means in my code, the declaration shall be visible at #1. In actually, typename T::type is a qualified-id, hence qualified name lookup rules apply to it and because T is a template parameter, So the lookup action shall be occurred after given a template argument, namely, during the instantiation of a specialization for such a template. But the quote I cited does not say anything about this. So, I wonder Is it a defect in the standard? If I miss anything that interpret this in the standard, please cite them for this question.

推荐答案

要完成language-lawyer 注释,在此页面


在模板定义中,可以推导出某些名称属于
的未知专业,特别是

Within a template definition, certain names are deduced to belong to an unknown specialization, in particular,


  • 合格名称,如果出现在左侧的话: :是非当前实例成员的从属类型

  • ...

在您的 struct TMP 中,


  • T 类型确实是依赖类型

  • 表达式 typename T ::类型是限定名称,左边的i f :: 是从属类型

  • 该表达式变为未知专业化

  • the type T is indeed a dependent type
  • the expression typename T::type is a qualified name and the left if :: is a dependent type
  • that expression becomes an unknown specialization

然后说:


专业领域不明的成员总是依赖,并且在实例化点被查找并绑定为所有依赖名称

Members of unknown specialization are always dependent, and are looked up and bound at the point of instantiation as all dependent names

,这允许查找 TMP< Test> :: type 实例的位置。

which allows the lookup of TMP<Test>::type at the point of instanciation.

这篇关于它是关于模板的依赖名称解析的标准中的缺陷吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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