编译器何时需要计算别名?

发布于 2025-01-27 05:11:22 字数 867 浏览 3 评论 0原文

考虑以下代码:

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 ++msvcintel正在以下选项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 for 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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫离伤花 2025-02-03 05:11:23

我假设t在这里是实际的非依赖类型,而不是另一个模板参数。


该行

using x = computation<T>;

不会导致计算的隐式实例化&lt; t&gt;。因此,编译器在这一点上没有理由尝试计算类型,特别是因为需要忽略任何实例化故障。如果类型的计算会产生无效的类型,或者以其他方式失败,则该程序可能无法编译。

该行

using y = computation<T>::type;

确实需要计算的隐式实例化&lt; t&gt;,因为将范围分辨率运算符应用于它。隐式实例化包括计算计算中的类型别名&lt; t&gt;。编译器必须执行计算,因为如果计算失败或产生无效的类型,则该程序将不正确,并且编译器需要诊断。

实际上,这并不取决于:: Type特别是部分。即使是:: type2对于其他类型别名,类模板专业化的隐式实例化也将需要计算type

同样,使用计算&lt; t&gt;在任何其他上下文中都需要完成的情况都需要隐式实例化,因此计算type,例如

auto z = computation<T>{};

I am assuming that T is an actual non-dependent type here and not another template parameter.


The line

using x = computation<T>;

does not cause implicit instantiation of 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. The program may not fail to compile if type's computation would yield an invalid type or would otherwise fail.

The line

using y = computation<T>::type;

does require implicit instantiation of computation<T> because the scope resolution operator is applied to it. The implicit instantiation includes computation of type aliases inside 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. Even if it was ::type2 for another type alias, the implicit instantiation of the class template specialization will require computation of type.

Similarly using computation<T> in any other context requiring it to be complete will require implicit instantiation and therefore computation of type, e.g.

auto z = computation<T>{};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文