c++ - 模板类的实例化点

标签 c++ templates c++14 instantiation token-name-resolution

该代码可以编译吗?

#include <iostream>

template <typename T>
struct TMPL
{
    using TP = typename T::TP; //is CL::TP visible (with T == CL)?
};

struct CL
{
    using TP = int;
    TMPL<CL>::TP val; 
};

int main()
{
    CL cl;
}

根据标准 14.6.4.1/4,TMPL 在 CL 类定义之前立即实例化

For a class template specialization, ..., if the specialization is implicitly instantiated because it is referenced from within another template specialization, .... Otherwise, the point of instantiation for such a specialization immediately precedes the namespace scope declaration or definition that refers to the specialization.

因此,CL::TP 在 TMPL 实例化点中不可见,但所有编译器(MSVC、gcc、clang)都可以很好地编译它。我还找到了缺陷报告 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#287 , 但它显然没有被接受

最佳答案

您的示例与 defect report 中的示例不同.在缺陷报告中,CL 是一个类模板。然而,提议的决议的目的是使模板案例与非模板案例相同,又名 [basic.scope.pdecl] :

6 After the point of declaration of a class member, the member name can be looked up in the scope of its class. [ Note: this is true even if the class is an incomplete class. For example,

struct X {
  enum E { z = 16 };
  int b[X::z];      // OK
};

end note ]

然后是提议的决议:

In 14.6.4.1 [temp.point] paragraph 3 change:

the point of instantiation is immediately before the point of instantiation of the enclosing template. Otherwise, the point of instantiation for such a specialization immediately precedes the namespace scope declaration or definition that refers to the specialization.

To:

the point of instantiation is the same as the point of instantiation of the enclosing template. Otherwise, the point of instantiation for such a specialization immediately precedes the nearest enclosing declaration. [Note: The point of instantiation is still at namespace scope but any declarations preceding the point of instantiation, even if not at namespace scope, are considered to have been seen.]

Add following paragraph 3:

If an implicitly instantiated class template specialization, class member specialization, or specialization of a class template references a class, class template specialization, class member specialization, or specialization of a class template containing a specialization reference that directly or indirectly caused the instantiation, the requirements of completeness and ordering of the class reference are applied in the context of the specialization reference.

截至最新draft ,非模板案例曾经并且仍然有效。模板案例不是。然而缺陷是drafting ,这意味着模板案例旨在编译。

Drafting: Informal consensus has been reached in the working group and is described in rough terms in a Tentative Resolution, although precise wording for the change is not yet available.

关于c++ - 模板类的实例化点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37742749/

相关文章:

c++ - 我如何决定是否为新的 C++ 项目使用 ATL、MFC、Win32 或 CLR?

C++:模板化继承递归类:不可能的三重威胁?

c++ - 可变参数模板整数序列的偏移量

c++ - 具有非数据类型模板参数的类模板特化

c++ - 如何将boost多精度整数从小端转换为大端?

c++ - 在单例中使用已删除的复制构造函数

android - 如何编译 Telegram jni文件夹

c++ - 如何从包含特定终止的子目录访问文件?

Java 开发人员学习企业 C++。等同于 JDBC 抽象?

templates - 在 vim-latex 套件 (Ubuntu) 上创建模板