Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

struct myclass
{
    int member;
    constexpr myclass(int input): member(input) {}
    constexpr void f() {member = 42;} // Is it allowed?
};
like image 264
Vincent Avatar asked Sep 21 '15 15:09

Vincent


People also ask

Can constexpr be modified?

According to the proposal, an object created within a constexpr expression, can now be changed during the evaluation process - until the evaluation process or the object's lifetime ends.

What is the use of constexpr in C++?

constexpr indicates that the value, or return value, is constant and, where possible, is computed at compile time. A constexpr integral value can be used wherever a const integer is required, such as in template arguments and array declarations.

Can a member function be constexpr?

const can only be used with non-static member functions whereas constexpr can be used with member and non-member functions, even with constructors but with condition that argument and return type must be of literal types.

Can constexpr throw exception?

Even though try blocks and inline assembly are allowed in constexpr functions, throwing exceptions or executing the assembly is still disallowed in a constant expression.


1 Answers

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:

its function-body shall be = delete, = default, or a compound-statement that contains only

  • null statements,
  • static_assert-declarations
  • typedef declarations and alias-declarations that do not define classes or enumerations,
  • using-declarations,
  • using-directives,
  • and exactly one return statement;

to a black-list:

its function-body shall be = delete, = default, or a compound-statement that does not contain

  • an asm-definition,
  • a goto statement,
  • 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:

Change: constexpr non-static member functions are not implicitly const member functions.

Rationale: Necessary to allow constexpr member functions to mutate the object.

Effect on original feature: Valid C++ 2011 code may fail to compile in this International Standard. 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:

struct S {
 constexpr const int &f();
 int &f();
};
like image 142
Shafik Yaghmour Avatar answered Oct 10 '22 00:10

Shafik Yaghmour