可以由编译器消除“未采用”的评估。分支在一个constexpr函数? [英] May a compiler elide the evaluation of the "not-taken" branch in a constexpr function?

查看:164
本文介绍了可以由编译器消除“未采用”的评估。分支在一个constexpr函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试回答Mehrdad的问题时,我已经调整了下面的小功能(在 liveworkspace ):

While attempting to answer a question by Mehrdad, I concocted the little function below (in action at liveworkspace):

template <typename T, unsigned low, unsigned high>
static constexpr auto highest_index_in() ->
   typename std::enable_if<high >= low, unsigned>::type
{
   return low == high                 ? low :
          high == low + 1             ? (exists<T, high>() ? high : low) :
          exists<T, (high + low)/2>() ? highest_index_in<T, (high+low)/2, high>() :
                                        highest_index_in<T, low, (high+low)/2>();
} // highest_index_in

(其中存在是O(1))

虽然(在liveworkspace上)编译是非常慢,并试图使用大范围失败与编译器崩溃$ c> [0,〜0u] 不起作用...)。

The compilation is extremely slow though (on liveworkspace), and attempting to use wide ranges fail utterly with compiler crashes ([0, ~0u] does not work...).

因此问题:当评估各种三元运算符在这里调用时,编译器可以省略计算

I believe I managed to implement the recursion correctly (I would be glad to be contradicted), and yet...

否,编译器不能跳过未采用的分支的评估。

Thus the question: when evaluating the various ternary operators calls here, may the compiler elide the computation of the not-taken branch ?

推荐答案

因为这样做意味着编译器首先必须确定在可能渲染程序不成形的那些分支中的任何分支中没有冲突的重载和/或模板专用化。为了做这个决定,编译器必须实例化分支上使用的模板并对函数执行重载解析。

No, the compiler can not skip the evaluation of the not-taken branches of the ternary operator, because to do so means the compiler first has to establish there are no conflicting overloads and/or template specialisations in any of those branches possible that could render the program ill-formed. To do that determination, the compiler effectively has to instantiate the templates used on the branches and perform overload resolution on the functions.

这篇关于可以由编译器消除“未采用”的评估。分支在一个constexpr函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆