Remove superfluous const parameters from function declarations #106
Replies: 4 comments 2 replies
-
As discussed in discord. |
Beta Was this translation helpful? Give feedback.
-
If consistency is the goal, then at least these offest parameters should be changed in void SetVertexBuffer(
ID3D11Buffer* vertexBuffer,
uint32_t vertexOffset);
void SetIndexBuffer(
ID3D11Buffer* indexBuffer,
uint32_t indexOffset); |
Beta Was this translation helpful? Give feedback.
-
clang-tidy has the same opinion on this issue. As non-reference and non-pointer arguments are passed by value and the caller therefore doesn't need to know whether the function implementation restricts the copied value to be const, it only makes sense to note the const declaration within the function definition, which usually will reside in the .cpp TU. The only point at which it is relevant knowing whether the value is const or not is within the function definition, and nowhere else is this information relevant. If you're writing an API you are focusing on writing within the source file, and the header file is only the interface to the outside and as they're not interested and have no reason to know whether it's const there is no reason for it to be included there. The header can be freed from any const, making the header easier to read while keeping the API functionally identical. I don't think this should be ignored simply for consistency, as it is a extra burden for writing and reading while bringing absolutely nothing to the table. Additionally, the header could be marked const while the definition isn't and the C++ standard didn't do this for no reason. It would also be possible to just add the definition to the header and mark it inline, if you want the const-ness to be visible to any callers. |
Beta Was this translation helpful? Give feedback.
-
Ok then lets remove const from hpp where applicable |
Beta Was this translation helpful? Give feedback.
-
For non-reference, non-pointer function prototype parameters,
const
is superfluous and has no effect. It only serves to clutter the API and does not provide any guarantee to the caller. See this SO answer for more info.Suggestion: remove
const
from non-reference, non-pointer-like parameters in function declarations, and keep them in function definitions to maintain const-correctness where it matters, for example:The other place I have noticed this issue is with the
std::string_view
parameter ofApplication
's (and its derivatives') constructor.Beta Was this translation helpful? Give feedback.
All reactions