+1 vote
in C Plus Plus by
Relaxed constexpr restrictions C++ 14 Features

1 Answer

0 votes
by
C++11 introduced the concept of a constexpr-declared function; a function which could be executed at compile time. Their return values could be consumed by operations that require constant expressions, such as an integer template argument. However, C++11 constexpr functions could only contain a single expression that is returned (as well as static_asserts and a small number of other declarations).

C++14 relaxes these restrictions. Constexpr-declared functions may now contain the following

Any declarations except:

static or thread_local variables.

Variable declarations without initializers.

The conditional branching statements if and switch.

Any looping statement, including range-based for.

Expressions which change the value of an object if the lifetime of that object began within the constant expression function. This includes calls to any non-const constexpr-declared non-static member functions.

goto statements are forbidden in C++14 relaxed constexpr-declared functions.

Also, C++11 stated that all non-static member functions that were declared constexpr were also implicitly declared const, with respect to this. That has since been removed; non-static member functions may be non-const.[6] However, per the restrictions above, a non-const constexpr member function can only modify a class member if that object's lifetime began within the constant expression evaluation.

Related questions

+1 vote
asked Jan 4, 2020 in C Plus Plus by AdilsonLima
+1 vote
asked Jan 4, 2020 in C Plus Plus by AdilsonLima
+1 vote
+1 vote
asked Jan 4, 2020 in C Plus Plus by AdilsonLima
...