gpt4 book ai didi

c++ - 如何强制在编译时评估 constexpr 函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:28:18 26 4
gpt4 key购买 nike

<分区>

给定以下代码:

constexpr int omg() { return 42; }

const int a = omg(); // NOT guaranteed to be evaluated at compile time

constexpr const int a = omg(); // guaranteed to be evaluated at compile time

有没有办法强制在编译时评估某些东西而不将其分配给某些 constexpr(或在编译时上下文中,如模板参数或枚举恶作剧)?

像这样:

const int a = force_compute_at_compile_time(omg());

也许是这样的(它不能编译——我对 constexpr 还不是很了解):

template<typename T> constexpr T force_compute_at_compile_time(constexpr const T& a) { return a; }

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com