In C ++ 14, can constexpr member change data member?

In C ++ 14, since it is constexprno longer indirectly const, can a member function constexprchange the class data member:

struct myclass
{
    int member;
    constexpr myclass(int input): member(input) {}
    constexpr void f() {member = 42;} // Is it allowed?
};
+4
source share
2 answers

Yes, it’s true, I believe that this change began with proposal N3598: constexpr member functions and implicit const, and eventually became part of N3652: relaxing restrictions on constexpr functions that changed the 7.1.5paragraph section 3, which is allowed in the body of the white list function :

= delete, = default ,

  • null,
  • static_assert-
  • typedef -, ,
  • ,
  • ,
  • return;

:

- = delete, = default ,

  • asm,
  • goto,
  • try
  • .

C.3.3 7: :

: constexpr - const -.

: , - constexpr .

: ++ 2011 . , ++ 2011, , - :

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

, . : [dcl.constexpr]:

constexpr :
- (10.3); - ;
- ; - = delete, = default ,

  • asm,
  • a goto,
  • try
  • .

.

+3

Source: https://habr.com/ru/post/1015725/


All Articles