编译器何时需要计算别名?
考虑以下代码:
template <class T>
struct computation {
using type = /* something based on T that takes time to compile */;
};
现在考虑两个代码:
using x = computation<T>;
和:
using y = typename computation<T>::type;
我想知道标准是否暗示:
- 选项A)任何“合理”编译器都会为
x <代码> y
- 选项b)编译器可以完全计算
计算&lt; t&gt; :: type
即使只有,
<强> 即使只有,
计算&lt; t&gt; 被调用,即使x
也会导致长期编译
时间“合理”编译器植入器的选项B。我知道该标准对编译器的实施一无所知,例如,如果它要求::类型
在专门调用之前不必存在,这将受到选项A。
Note注意。 :根据我的经验,我很确定g ++
,clang ++
,msvc
和intel
正在以下选项a),但我不知道它是否仅仅是纯粹的运气与标准中的事物有关。
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 fory
- Option B) A compiler could totally compute
computation<T>::type
even if onlycomputation<T>
is called, leading to long compile-time even forx
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. 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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设
t
在这里是实际的非依赖类型,而不是另一个模板参数。该行
不会导致
计算的隐式实例化&lt; t&gt;
。因此,编译器在这一点上没有理由尝试计算类型
,特别是因为需要忽略任何实例化故障。如果类型
的计算会产生无效的类型,或者以其他方式失败,则该程序可能无法编译。该行
确实需要
计算的隐式实例化&lt; t&gt;
,因为将范围分辨率运算符应用于它。隐式实例化包括计算计算中的类型别名&lt; t&gt;
。编译器必须执行计算,因为如果计算失败或产生无效的类型,则该程序将不正确,并且编译器需要诊断。实际上,这并不取决于
:: Type
特别是部分。即使是:: type2
对于其他类型别名,类模板专业化的隐式实例化也将需要计算type
。同样,使用
计算&lt; t&gt;
在任何其他上下文中都需要完成的情况都需要隐式实例化,因此计算type
,例如I am assuming that
T
is an actual non-dependent type here and not another template parameter.The line
does not cause implicit instantiation of
computation<T>
. There is therefore no reason for a compiler to try to computetype
at this point, in particular since any instantiation failure would need to be ignored. The program may not fail to compile iftype
's computation would yield an invalid type or would otherwise fail.The line
does require implicit instantiation of
computation<T>
because the scope resolution operator is applied to it. The implicit instantiation includes computation of type aliases insidecomputation<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. Even if it was::type2
for another type alias, the implicit instantiation of the class template specialization will require computation oftype
.Similarly using
computation<T>
in any other context requiring it to be complete will require implicit instantiation and therefore computation oftype
, e.g.