确定`constexpr`执行 - 在编译期间或在运行时? [英] Determine `constexpr` execution - during compilation or at runtime?

查看:137
本文介绍了确定`constexpr`执行 - 在编译期间或在运行时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编译阶段和运行时有没有办法实现 constexpr 函数的不同行为?

Is there a way to achieve different behaviour of a constexpr function in the compilation phase and at runtime?

请考虑以下示例(使用 D:static if 的理论特性):

Consider the following example (using a theoretical feature from D: static if):

constexpr int pow( int base , int exp ) noexcept
{
    static if( std::evaluated_during_translation() ) {
        auto result = 1;
        for( int i = 0 ; i < exp ; i++ )
            result *= base;
        return result;
    } else { // std::evaluated_during_runtime()
        return std::pow( base , exp );
    }
}

如果没有,是否有办法限制 constexpr 只有编译时间吗?

If not, is there a way to restrict constexpr to be compile-time only?

推荐答案

很抱歉。

N3583是一篇论文,建议修改以允许您要求的内容。

N3583 is a paper proposing changes to allow what you are asking for.

这篇关于确定`constexpr`执行 - 在编译期间或在运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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