简体   繁体   English

在C ++ 14中,constexpr成员可以更改数据成员吗?

[英]In C++14 can a constexpr member change a data member?

In C++14, since constexpr are not implicitly const anymore, can a constexpr member function modify a data member of a class: 在C ++ 14中,由于constexpr不再是隐式const ,因此constexpr成员函数可以修改类的数据成员:

struct myclass
{
    int member;
    constexpr myclass(int input): member(input) {}
    constexpr void f() {member = 42;} // Is it allowed?
};

As far as I can tell, yes. 据我所知,是的。 The restrictions are, from [dcl.constexpr]: 限制来自[dcl.constexpr]:

The definition of a constexpr function shall satisfy the following constraints: constexpr函数的定义应满足以下约束:
— it shall not be virtual (10.3); - 它不应是虚拟的(10.3);
— its return type shall be a literal type; - 其返回类型应为字面类型;
— each of its parameter types shall be a literal type; - 每个参数类型都应为文字类型;
— its function-body shall be = delete , = default , or a compound-statement that does not contain - 它的函数体应为= delete= default或不包含的复合语句

  • an asm-definition , asm-definition
  • a goto statement, 一个goto声明,
  • a try-block , or 尝试块 ,或
  • a definition of a variable of non-literal type or of static or thread storage duration or for which no initialization is performed. 非文字类型或静态或线程存储持续时间的变量的定义,或者不执行初始化的定义。

The function meets all those requirements. 该功能满足所有这些要求。

Yes they are, I believe this change started with proposal N3598: constexpr member functions and implicit const and eventually became part of N3652: Relaxing constraints on constexpr functions which changed section 7.1.5 paragraph 3 what is allowed in the function body from a white-list: 是的,我相信这个改变始于提议N3598:constexpr成员函数和隐式const并最终成为N3652的一部分:放宽对co​​nstexpr函数的约束,这改变了第7.1.5节第3段功能体中允许的白色 -列表:

its function-body shall be = delete, = default, or a compound-statement that contains only 它的函数体应为= delete,= default或仅包含的复合语句

  • null statements, 空语句,
  • static_assert-declarations static_assert申述
  • typedef declarations and alias-declarations that do not define classes or enumerations, typedef声明和不定义类或枚举的别名声明,
  • using-declarations, 使用申述,
  • using-directives, using指令,
  • and exactly one return statement; 而且只有一个退货声明;

to a black-list: 到黑名单:

its function-body shall be = delete, = default, or a compound-statement that does not contain 它的函数体应为= delete,= default或不包含的复合语句

  • an asm-definition, asm-definition,
  • a goto statement, 一个goto声明,
  • a try-block, or 尝试块,或
  • a definition of a variable of non-literal type or of static or thread storage duration or for which no initialization is performed. 非文字类型或静态或线程存储持续时间的变量的定义,或者不执行初始化的定义。

and also added the following notes to section C.3.3 Clause 7: declarations: 并在C.3.3第7节中增加了以下注释:声明:

Change: constexpr non-static member functions are not implicitly const member functions. 更改:constexpr非静态成员函数不是隐式const成员函数。

Rationale: Necessary to allow constexpr member functions to mutate the object. 基本原理:必须允许constexpr成员函数改变对象。

Effect on original feature: Valid C++ 2011 code may fail to compile in this International Standard. 对原始功能的影响:有效的C ++ 2011代码可能无法在本国际标准中编译。 For example, the following code is valid in C++ 2011 but invalid in this International Standard because it declares the same member function twice with different return types: 例如,以下代码在C ++ 2011中有效,但在本国际标准中无效,因为它使用不同的返回类型声明了两次相同的成员函数:

 struct S { constexpr const int &f(); int &f(); }; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM