-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[C++] Top-level const
and volatile
should not be present for function parameters
#10079
Comments
const
and volatile
should not be visible for function parametersconst
and volatile
should not be present for function parameters
Some thoughts:
|
I disagree. It doesn't create an inconsistency in user code: it's a feature of the language because declarations can be removed from definitions. When creating a proper declaration, there is no benefit to mentioning that a parameter is top-level cv-qualified because from the user's perspective, any argument passed in won't be modified. Whether or not it's modified in the definition is immaterial because the user will never experience that.
If you're interested in following up on this in the grammar, here's the first/follow set.
[dcl.fct]/p5 provides the standard wording for this section. |
I think we have here a small misunderstanding what I meant is that the user uses 2 styles which by the compiler / language standard are mapped to the same function. For the reader it might be initially confusing whether or not e.g Regarding the links, it would be better to refer here to the paragraphs / codes (like |
@albert-github I do understand the bug report. If you have a declaration void foo(int x); this is exactly the same as void foo(const int x); since However consider the following valid definition of either declaration: void foo(int x)
{
x = 42; // this is ok, overwrites x
} then the above is fine, but the same example with const is an error, not in the declaration, but in the implementation: void foo(const int x)
{
x = 42; // compiler error: x is const and cannot change.
} So the const in the definition is part of the implementation, not of the user facing API, so one can argue if it should appear in the documentation or not. See also https://stackoverflow.com/questions/117293/use-of-const-for-function-parameters for more discussion on this topic. Note that things can get a bit difficult for doxygen if you start to hide reference types behind typedefs or macros, e.g. #if PASS_BY_VALUE
#define MyType int
#else
#define MyType int *
#endif
void foo(const MyType x); // is this const redundant or not? it depends... |
Clang's parser is able to detect type aliases, and there's even a Clang Tidy check. Macros might be a bit more tricky. It might be possible in the above case, but this would be borderline impossible. #if PASS_BY_VALUE
using MyType = int;
#else
using MyType = int*;
#endif As much as I would prefer it to be not present by default, perhaps there should be an opt-in for this? |
@doxygen |
It might mean what you've said. It might not. Perhaps the author could have originally intended this, then changed the definition so that the parameter is no longer Having said that, none of this addresses that the user does not benefit from knowing that a parameter is cv-qualified at the top level. My users have no need to know that I've chosen to /// Returns ``cmp == 0``.
constexpr bool is_eq(partial_ordering const cmp) noexcept
{
return cmp == 0;
} without
None of these are satisfactory solutions to the problem.
It's not lazy to pick one good name for something and then use that for the duration of the scope.
Not sure what you're talking about here. Compilers don't create locals when they don't need to, and optimised builds will remove local variables if they have the opportunity. Here's a demonstration. |
a good local variable could be
What I meant to say with "probably creates a local variable" that it conceptually creates, not shown to the user, something like a local / scratch variable that is discarded when the routine / block is finished. How the compiler does this is immaterial, it can be on the stack, in a register etc. The compiler somewhere has to store temporarily the information. |
Describe the bug
void f(int)
,void f(int const)
, andvoid f(int volatile)
are all the declarations for the same overload A C++ best-practice is to notconst
-qualify proper function declarations, and only function definitions. This is because one can declaref
asvoid f(int const)
, and then define it asvoid f(int)
. You can verify this using this Compiler Explorer example.Note that this does not apply to references.
void f(int&)
andvoid f(int const&)
are different overloads, as demonstrated by this second Compiler Explorer example.Top-level
const
-qualifier is redundant at best, but it's potentially misleading, since a reader may interpret this to mean thatx
won't change, which isn't guaranteed by a proper declaration, since the definition can change this up.Functions with inline linkage are typically defined in header files, which means that the Doxygen will be like so:
For the parameters, Doxygen generates the following.
Expected behavior
The above XML should be
To Reproduce
square
to the C++ file being processed.Version
Stack trace
n/a
The text was updated successfully, but these errors were encountered: