Question Details

No question body available.

Tags

c++ constants constexpr noexcept cpp-core-guidelines

Answers (2)

Accepted Answer Available
Accepted Answer
August 17, 2025 Score: 8 Rep: 8,479 Quality: Expert Completeness: 60%

But isn't all that true for const as well? When I declare a function parameter as const&, am I not making a decision about the interface that I will not be able to revert in the future because it would break all my clients?

Yes.

One needs to actually put effort into designing good interfaces. Try to approach it as: could an implementation reasonably need this power?

So if throwing is impossible because there's no way for the function's operation, not the current implementation, to fail or because you have another way to communicate the only types of failures... use noexcept.

Likewise, if there's a possibility a function may need to return information and there's no other channel to do so... Use a pointer or reference to non-const. But the guidelines also favor using the actual return type to return information, over out or in-out parameters, so you shouldn't find yourself in this situation as often. Almost to the point that if you think you could treat a parameter as in-out, you probably already are going so (and modifying it).

EDIT: That's how you can approach the decision. You also ask why you might feel differently about your decisions in the future in the two cases.

Suppose you were too conservative and believed you didn't need to modify a parameter, but now you do. You change the parameter to take by reference instead of reference to const. How is the rest of your system affected? If the caller already had a modifiable value, it can pass it. If it didn't, compilation fails. This is the safest failure mode when changing code.

Now, take noexcept. Suppose you were too conservative and added noexcept because you believed a function did not need to throw. Now, you want to throw so you remove noexcept. How is the rest of the system affected? It compiles. No errors. If called from another noexcept function, previously it was perfectly safe. Now, it still compiles, but it sometimes crashes (via std::terminate). You added a bug.

August 27, 2025 Score: 1 Rep: 212,690 Quality: Low Completeness: 40%

noexcept doesn't mean the function promises not to throw.

It means the compiler promises to call std::terminate if your function does throw.

This is rarely the guarantee you actually want, and it can slow down or bloat the generated program.

On top of that, you're preventing future implementations from throwing, which they might need to do for reasons you might not imagine at the time of authorship.