c++ - gcc 和 clang 在运算符重载解析期间隐式实例化模板参数

标签 c++ templates gcc clang

考虑这段代码:

struct A; // incomplete type

template<class T>
struct D { T d; };

template <class T>
struct B { int * p = nullptr; };

int main() {
    B<D<A>> u, v;
    u = v;  // doesn't compile; complain that D<A>::d has incomplete type
    u.operator=(v); // compiles
}

Demo .从 u.operator=(v)编译但 u = v;不是,在后一个表达式的重载解析期间,编译器必须隐式实例化 D<A> - 但我不明白为什么需要实例化。

为了让事情更有趣,这段代码编译:

struct A; // incomplete type

template<class T>
struct D; // undefined

template <class T>
struct B { int * p = nullptr; };

int main() {
    B<D<A>> u, v;
    u = v;
    u.operator=(v);
}

Demo .

这里发生了什么?为什么u = v;导致 D<A> 的隐式实例化- B 的正文中没有使用的类型的定义——在第一种情况下,而不是第二种情况?

最佳答案

整件事的重点是ADL开始:

N3797 - [basic.lookup.argdep]

When the postfix-expression in a function call (5.2.2) is an unqualified-id, other namespaces not considered during the usual unqualified lookup (3.4.1) may be searched, and in those namespaces, namespace-scope friend function or function template declarations (11.3) not otherwise visible may be found.

以下:

For each argument type T in the function call, there is a set of zero or more associated namespaces and a set of zero or more associated classes to be considered. [...] The sets of namespaces and classes are determined in the following way:

  • If T is a class type [..] its associated classes are: ... furthemore if T is a class template specialization its associated namespaces and classes also include: the namespaces and classes associated with the types of the template arguments provided for template type parameters

D<A>是一个关联类,因此在列表中等待轮到它。

现在是有趣的部分 [temp.inst]/1

Unless a class template specialization has been explicitly instantiated (14.7.2) or explicitly specialized (14.7.3), the class template specialization is implicitly instantiated [...] when the completeness of the class type affects the semantics of the program

可以认为 D<A> 类型的完整性完全不影响该程序的语义,但是 [basic.lookup.argdep]/4 说:

When considering an associated namespace, the lookup is the same as the lookup performed when the associated namespace is used as a qualifier (3.4.3.2) except that:

[...] Any namespace-scope friend functions or friend function templates declared in associated classes are visible within their respective namespaces even if they are not visible during an ordinary lookup (11.3)

即类类型的完整性实际上会影响 friend 声明 -> 类类型的完整性因此会影响程序的语义。 这也是您的第二个示例有效的原因。

TL;DR D<A>被实例化了。

最后一个有趣的点是为什么 ADL 首先是为了

u = v; // Triggers ADL
u.operator=(v); // Doesn't trigger ADL

§13.3.1.2/2 规定不能有非成员(member) operator= (内置的除外)。加入 [over.match.oper]/2:

The set of non-member candidates is the result of the unqualified lookup of operator@ in the context of the expression according to the usual rules for name lookup in unqualified function calls (3.4.2) except that all member functions are ignored.

逻辑结论是:如果表 11 中没有非成员形式,则执行 ADL 查找毫无意义。但是 [temp.inst]p7 放宽了这一点:

If the overload resolution process can determine the correct function to call without instantiating a class template definition, it is unspecified whether that instantiation actually takes place.

这就是 clang 触发整个 ADL -> implicit instantiation 的原因。首先处理。

r218330 开始(在撰写本文时,它已在几分钟前提交)此行为已更改为不对 operator= 执行 ADL完全没有。


引用文献

感谢 Richard Smith 和 David Blaikie 帮助我解决这个问题。

关于c++ - gcc 和 clang 在运算符重载解析期间隐式实例化模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25925551/

相关文章:

c++ - 预处理器魔术来完成模板中的类型名

c - 用gcc内联C代码中的所有函数

c - 分配var = 0.0时程序退出;

c++ - 关于 std::move 行为

c++迭代模板函数接受的对象

c++ - GeoDjango GDAL 安装

c++ - 将指向类成员的指针作为模板参数传递

c - LD_LIBRARY_PATH 被 GCC 忽略

c++ - 如何计算 OpenCV 中矩阵的直方图?

c++ - ld : symbol(s) not found for architecture x86_64 - Error