Usage of std::is_constant_evaluated() since C++23
Asked Answered
A

1

6

I understand that std::is_constant_evaluated() was useful to determine compile time evaluation in C++20.

But since C++23 we have if consteval.

However there is no mention of deprecation for std::is_constant_evaluated().

Is there any practical usage for it starting from C++23 ?

Araceli answered 2/10 at 14:3 Comment(4)
I believe that std::is_constant_evaluated() serves to determine from within the scope of the function whether it is declared as consteval. if consteval is a different thingBernhard
I guess you can use it in expressions, like const auto x = std:is_contant_evaluated() ? something : other;Prudie
@SergeyKolesnik the "possible implementation" section of std::is_constant_evaluated actually uses if consteval starting from C++23. Why do you think it is different ?Araceli
@Araceli Not just possible implementation, it is now the actual specification.Foraminifer
F
6

Quoting from the paper:

One of the questions that comes up regularly in discussing this paper is: if we had if consteval, we do we even need std::is_constant_evaluated(), and can we just deprecate it?

This paper proposes no such deprecation. The reason is that this function is actually still occasionally useful (as in the previous section). If the standard library does not provide it, users will write their own. We’re not concerned about the implementation difficulty of it - the users that need this will definitely be able to write it correctly - but we are concerned with a proliferation of exactly this function. The advantage of having the one std::is_constant_evaluated() is both that it becomes actually teachable and also that it becomes warnable: the warnings discussed can happen only because we know what this name means. Maybe it’s still possible to warn on if constexpr (your::is_constant_evaluated()) but that’s a much harder problem.

And note that libstdc++ already has some uses that do require the function form.


Edit: Even though I had specific anchored github links there and not just to master, those "some uses" both point to lines that... are not actual uses of the form I was talking about. Not sure what happened there...

Foraminifer answered 2/10 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.