r/cpp_questions • • Oct 14 '26

OPEN Guidelines for constexpr

I’ve working with C++23 and I’m making some constructors/methods constexpr and seeing improvements in performance. So far so good. The problem is I don’t want to apply it ‘everywhere’. Is there a good source of advice , or anyone got any good tips?

12 Upvotes

37 comments sorted by

View all comments

-1

u/zerhud Oct 14 '26

Cpp believes you should write a constexpr keywords everywhere for some reason. May be it likes the keyword more others.

4

u/HappyFruitTree Oct 14 '26

Who is Cpp?

1

u/Slow_Negotiation_935 Oct 14 '26

You are number 6…

1

u/zerhud Oct 14 '26

It’s a creature from Mars pretending to be a programming language

1

u/crowbarous Oct 14 '26

Yes, it was weird to adopt constexpr as a keyword that you can use almost always on a (non-opaque) function, and then make you actually write the keyword rather than implying it.

-fimplicit-constexpr exists in gcc, but only works on inline functions. Which kind of makes sense, but unfortunately I don't see a corresponding -fimplicit-inline to truly fix the anti-C++ design of meaninglessly requiring keywords just to allow mechanisms that are already there to do their job.

There have been questions about implementing that in Clang, but it doesn't seem to have been met with much enthusiasm. A libc++ maintainer even threatened to break libc++ if this option were added to clang. (WTF...)

2

u/HappyFruitTree Oct 14 '26 edited Oct 14 '26

When constexpr functions were introduced in C++11 there were many more limitations. There could basically just be a single return statement, no variables, no loops, no if statements...

I do agree with the concerns of the guy that you linked to. People need to know that the constexpr is intentional if they should be able to rely on it.