Given a very trivial function,int transform(int val) {    return (val + 7) / 8;}It should be very obvious that it's easy to turn this function into a constexpr function, allowing me to use it when defining constexpr variables, like so:constexpr int transform(int val) {    return (val + 7) / 8;}My assumption is that this is strictly an improvement, since the function can still be called in a non-constexpr context, and it can now also be used to help define compile-time constant variables.My question is, are there situations where this is a bad idea? Like, by making this function constexpr, can I ever encounter a situation where this function will no longer be usable in a particular circumstance, or where it will misbehave?
Is it ever bad to mark a C++ function constexpr?
c++;c++11
This matters only if the function is part of a public interface, and you want to keep future versions of your API binary-compatible. In that case, you have to think carefully how you want to evolve your API, and where you need extension points for future changes.That makes a constexpr qualifier an irrevocable design decision. You cannot remove this qualifier without an incompatible change to your API. It also limits how you can implement that function, e.g. you would not be able to do any logging within this function. Not every trivial function will stay trivial in eternity.That means you should preferably use constexpr for functions that are inherently pure functions, and that would be actually useful at compile time (e.g. for template metaprogramming). It would not be good to make functions constexpr just because the current implementation happens to be constexpr-able.Where compile-time evaluation is not necessary, using inline functions or functions with internal linkage would seem more appropriate that constexpr. All of these variants have in common that the function body is public and is available in the same compilation unit as the call location.If the function in question is not part of a stable, public API, this is less of an issue since you can arbitrarily change the design at will. But since you now control all call sites, it is not necessary to mark a function constexpr just in case. You know whether you are using this function in a constexpr context. Adding unnecessarily restrictive qualifiers might then be considered obfuscation.