kingsjester
kingsjester

Reputation: 308

Is it possible to modify objects with non-local scope in constexpr function since c++14?

I read the book "C++ Concurrency in Action, 2nd Edition" (from Anthony Williams), and I'm not sure of one sentence in appendix A.4.1 :

"But that's about all you can do in C++11 - a constexpr function can only call other constexpr functions. In C++14, this restriction is lifted, and you can do almost anything in a constexpr function, provided it doesn't modify any objects with non local scope."

In this thread from 2015, someone also states that it is not allowed to modify non-local objects in a constexpr function :

If you want to write a function that isn't allowed to modify any non-local variables at all, you can declare it constexpr, although that also imposes additional restrictions.

This statement seems contradicted by this SO thread. In it, there is a a constexpr function that modify a variable from outer scope by passing by reference, the code compiles and somebody cites a paper which says that since c++14 :

A constant expression may still have side-effects which are local to the evaluation and its result.

So now I'm confused : Is it possible (and authorized by the standard) since c++14 to modify objects with non-local scope in constexpr function ?

Upvotes: 0

Views: 38

Answers (0)

Related Questions