-
-
Notifications
You must be signed in to change notification settings - Fork 881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Const as Much as Possible (Not!) #128
Comments
Dan Saks gives a better, and more thorough, description of this topic in the article "Top-Level cv-Qualifiers in Function Parameters" [PDF]. What I called "direct", he calls "top-level". |
I assume you mean only for parameters passed "by value"? if the parameter is pass by reference then surely |
Yes. But just to be explicit in our agreement about where |
While "Const as Much as Possible" is generally good practice,
const
should not be directly used on function declaration parameters, including member functions.A quick example:
Notice I said "directly". Hence
s
isn'tconst
in the declaration, but is in the definition (direct), but whats
points at isconst
in both, and must match in both (indirect).The reason for doing this is to not leak a function's implementation details. Because whether a function treats a parameter variable as
const
or not is an implementation detail of the function. And if a function gets modified such that its parameter is no longer treated asconst
, then callers of that function shouldn't have to be recompiled.This is a difference between C and C++. In C, the function definition's parameter type(s) must exactly match that of the function declarations. (At least it used to. I haven't actually developed anything significant with modern C.)
The text was updated successfully, but these errors were encountered: