编译器是否可以在 constexpr 函数中省略"not-taken"分支的计算?
May a compiler elide the evaluation of the "not-taken" branch in a constexpr function?
在试图回答Mehrdad的问题时,我编写了下面的小函数(在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
(其中exists
= 0 (1))
编译非常慢(在liveworkspace上),并且试图使用宽范围完全失败,编译器崩溃([0, ~0u]
不工作…)。
我相信我正确地实现了递归(我很高兴有人反驳),但是…
因此问题来了:在计算这里的各种三元操作符调用时,编译器是否可以忽略未取分支的计算?不,编译器不能跳过三元操作符的未取分支的求值,因为这样做意味着编译器首先必须确定在任何可能导致程序格式错误的分支中没有冲突的重载和/或模板特化。为了进行这种判断,编译器必须有效地实例化分支上使用的模板,并对函数执行重载解析。
相关文章:
- IPC使用多个管道和分支进程来运行Python程序
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- 如何删除peer if else分支中的冗长句子
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 如何正确地将分支添加到已存在的树中
- 使用JsonCpp将数据返回到带有pybind11的python会在python调用中产生Symbol not foun
- OpenCV Android C++ imwrite not found
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 使用单词"not"作为C ++类的名称会导致VS2019错误
- 错误"Could not find Boost"(缺少:上下文标头)
- 如何将分支添加到已存在的TTree:ROOT
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- Is !NaN not a NaN?
- 为什么我会" void value not ignored as it ought to be"?
- 如何删除 LLVM 中的不规则分支?
- 解决"ld: library not found for -ltensorflow_framework.2.3.0"
- 加载与引用 .NET DLL 位于同一文件夹中的引用的 .NET DLL 时"Not found"异常
- Directx 11 - CompileFromFile() is not compiling
- 编译器是否可以在 constexpr 函数中省略"not-taken"分支的计算?