简体   繁体   English

编译器什么时候需要计算别名?

[英]When does the compiler need to compute an alias?

Consider the following code:考虑以下代码:

template <class T>
struct computation {
    using type = /* something based on T that takes time to compile */;
};

Now consider two codes:现在考虑两个代码:

using x = computation<T>;

and:和:

using y = typename computation<T>::type;

I am wondering whether the standard implies that:我想知道该标准是否意味着:

  • Option A) Any "reasonable" compiler will lead to a quick compile time for x and long compile time for y选项 A)任何“合理”的编译器都会导致x的编译时间较快,而y的编译时间较长
  • Option B) A compiler could totally compute computation<T>::type even if only computation<T> is called, leading to long compile-time even for x选项 B)即使只调用computation<T> ,编译器也可以完全计算computation<T>::type ,导致编译时间很长,即使对于x

In other words, I am trying to know if the standard specifies anything that would most likely translate into option A or option B for a "reasonable" compiler implemeter.换句话说,我想知道该标准是否指定了最有可能转化为“合理”编译器实现表的选项 A 或选项 B 的任何内容。 I know that the standard says nothing about compiler implementation but for example if it requires that ::type does not have to exist until it's specifically called, that would be in favor of option A.我知道该标准对编译器实现没有任何说明,但是例如,如果它要求::type在被专门调用之前不必存在,那将有利于选项 A。

NOTE: In my experience, I am pretty sure that g++ , clang++ , msvc , and intel are following option A), but I have no idea whether it's just by pure luck of it's related to something in the standard.注意:根据我的经验,我很确定g++clang++msvcintel都遵循选项 A),但我不知道它是否只是运气好,它与标准中的某些内容有关。

I am assuming that T is an actual non-dependent type here and not another template parameter.我假设T在这里是一个实际的非依赖类型,而不是另一个模板参数。


The line线路

using x = computation<T>;

does not cause implicit instantiation of computation<T> .不会导致computation<T>的隐式实例化。 There is therefore no reason for a compiler to try to compute type at this point, in particular since any instantiation failure would need to be ignored.因此,编译器没有理由在此时尝试计算type ,特别是因为任何实例化失败都需要被忽略。 The program may not fail to compile if type 's computation would yield an invalid type or would otherwise fail.如果type的计算会产生无效类型或以其他方式失败,程序可能不会编译失败。

The line线路

using y = computation<T>::type;

does require implicit instantiation of computation<T> because the scope resolution operator is applied to it.确实需要computation<T>的隐式实例化,因为 scope 解析运算符应用于它。 The implicit instantiation includes computation of type aliases inside computation<T> .隐式实例化包括在computation<T>中计算类型别名。 The compiler must perform the computation, because if the computation failed or would yield an invalid type, then the program would be ill-formed and the compiler would need to diagnose it.编译器必须执行计算,因为如果计算失败或产生无效类型,则程序将是病式的,编译器需要对其进行诊断。

This doesn't actually dependent on the ::type part specifically.这实际上并不具体依赖于::type部分。 Even if it was ::type2 for another type alias, the implicit instantiation of the class template specialization will require computation of type .即使它是另一个类型别名的::type2 type2,class 模板特化的隐式实例化也需要计算type

Similarly using computation<T> in any other context requiring it to be complete will require implicit instantiation and therefore computation of type , eg类似地,在任何其他要求它完整的上下文中使用computation<T>将需要隐式实例化,因此需要计算type ,例如

auto z = computation<T>{};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM