constexpr是否暗示内联? [英] Does constexpr imply inline?

查看:325
本文介绍了constexpr是否暗示内联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下内联函数:

// Inline specifier version
#include<iostream>
#include<cstdlib>

inline int f(const int x);

inline int f(const int x)
{
    return 2*x;
}

int main(int argc, char* argv[])
{
    return f(std::atoi(argv[1]));
}

和constexpr等效版本:

and the constexpr equivalent version :

// Constexpr specifier version
#include<iostream>
#include<cstdlib>

constexpr int f(const int x);

constexpr int f(const int x)
{
    return 2*x;
}

int main(int argc, char* argv[])
{
    return f(std::atoi(argv[1]));
}

我的问题是: constexpr 指定符表示 inline 说明符,意味着如果非常量参数传递给 constexpr 函数,编译器将尝试 inline 该函数,如同 inline 说明符放在其声明中?

My question is : does the constexpr specifier imply the inline specifier in the sense that if a non-constant argument is passed to a constexpr function, the compiler will try to inline the function as if the inline specifier was put in its declaration ?

C ++ 11标准是否保证?

Does the C++11 standard guarantee that ?

推荐答案

7.1.5 / 2):constexpr函数和constexpr构造函数是隐式内联(7.1.2)。

Yes (§7.1.5/2): "constexpr functions and constexpr constructors are implicitly inline (7.1.2)."

但请注意, inline 说明符确实有非常 )对编译器是否可能内联扩展函数的影响。然而,它确实影响了一个定义规则,从这个角度来看,编译器需要遵循与 constexpr 函数相同的规则作为 inline function。

Note, however, that the inline specifier really has very little (if any) effect upon whether a compiler is likely to expand a function inline or not. It does, however, affect the one definition rule, and from that perspective, the compiler is required to follow the same rules for a constexpr function as an inline function.

我还应该补充一点,不管 constexpr $ c> inline , constexpr 函数的规则要求它们足够简单,以至于它们通常是内联扩展的良好候选对象是递归的)。

I should also add that regardless of constexpr implying inline, the rules for constexpr functions require them to be simple enough that they're often good candidates for inline expansion (the primary exception being those that are recursive).

这篇关于constexpr是否暗示内联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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