“ constexpr”有什么用? [英] What are 'constexpr' useful for?

查看:218
本文介绍了“ constexpr”有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的找不到任何用途。我的第一个想法是,我可以使用它来实现按合同设计,而无需使用这样的宏:

I really can't find any use of it. My first idea was that I could use it to implement 'Design by Contract' without using macros like this:

struct S
{   
    S(constexpr int i) : S(i) { static_assert( i < 9, "i must be < 9" ); }

    S(int i); //external defintion

    char *pSomeMemory;
};

但这不会编译。我想我们也可以使用它来引用同变量,而当我们想要避免将get / setter设为从用户到一个成员的实例为只读时,不需要创建额外的内存:

But this wouldn't compile. I thought we could also use it to reference same-variable without the need of additional memory to be created when we want to avoid the get/setters in order to make instances to one member from users to be read-only:

class S
{  
private:
    int _i;

public:
    const int & constexpr i = _i;
};

但以上内容均未实际编译。

But none of the above actually compiled. Can someone give me some insight why this keyword was being introduced?

推荐答案

constexpr的目标 code>取决于上下文:

The goal of constexpr depends on the context:


  1. 对于对象,它表示对象是不可变的,应在编译时构造-时间。除了将操作移至编译时而不是在运行时执行操作之外,创建 constexpr 对象还有一个额外的优势,即它们可以在创建任何线程之前进行初始化。结果,他们的访问不再需要任何同步。将对象声明为 constexpr 的示例如下所示:

  1. For objects it indicates that the object is immutable and shall be constructed at compile-time. Aside from moving operations to compile-time rather than doing them at run-time creating constexpr objects has the added advantage that they are initialize before any threads are created. As a result, their access never needs any synchronization. An example of declaring an object as constexpr would look like this:

constexpr T value{args};

很明显,为了起作用, args

Obviously, for that to work, args need to be constant expressions.

对于函数,它表明调用该函数可以生成一个常量表达式。 constexpr 函数调用的结果是否产生常量表达式取决于函数的参数和定义。直接的含义是该函数必须是 inline (将隐式如此)。另外,在此功能内可以完成的操作受到限制。对于C ++ 11,该函数只能具有一个语句,对于非构造函数,该语句必须为 return 语句。此限制在C ++ 14中得到了放松。例如,以下是 constexpr 函数的定义:

For functions it indicates that calling the function can result in a constant expression. Whether the result of constexpr function call results in a constant expression depends on the arguments and the definition of the function. The immediate implication is that the function has to be inline (it will implicitly be made so). In addition, there are constraints on what can be done within such a function. For C++11 the function can have only one statement which, for non-constructors, has to be a return-statement. This restriction got relaxed in C++14. For example, the following is a definition of a constexpr function:

constexpr int square(int value) { return value * value; }


创建时非内置类型的constexpr 对象,相应的类型将需要一个 constexpr 构造函数:生成的默认构造函数将不起作用。显然, constexpr 构造函数将需要初始化所有成员。 constexpr 构造函数看起来像这样:

When creating constexpr object of non-built-in types the respective types will need a constexpr constructor: the generated default constructor won't work. Obviously, a constexpr constructor will need to initialize all members. A constexpr constructor could look like this:

struct example {
    int value;
    constexpr example(int value): value(value) {}
};

int main() {
    constexpr example size{17};
    int array[size.value] = {};
}

创建的 constexpr 值可以在任何需要常量表达式的地方使用。

The created constexpr values can be used everywhere a constant expression is expected.

这篇关于“ constexpr”有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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