What does the second const do in "const int const & someval"?

talk about a function parameter, for example:

void doSomething(const int const &someVal);

      

As I understand it, the first constant indicates that the value will not be changed by the function, but rather indicates that the value should be passed by reference. But what does the second "const" do?

+3


source to share


2 answers


As per the C ++ standard (7.1.6.1 cv qualifiers)

1 There are two cv qualifiers, const and volatile. Each cv qualifier must appear at most once in cvqualifier-seq . If the cv-qualifier appears in a seq-qualifier-declaration, the declaration must not be empty in the init-declarator-list. [Note: 3.9.3 and 8.3.5 describe how cv qualifiers affect object and function types. -end note] Fallback cv qualifications are ignored. [Note: for example they can be entered with typedefs.-end note]

So this is an ad

void doSomething(const int const &someVal);

      



has a redundant constant specifier that is simply ignored.

The second const qualifier would make sense if someVal

declared as a reference to a const pointer. for example

void doSomething(const int * const &someVal);

      

+2


source


Both should work:

void doSomething(const int &someVal);

void doSomething(int const &someVal);

      



Using the second constant does not add any functionality, and the additional one should be removed. It's good practice to keep the const to the left, though for clarity, as it stands out better.

0


source







All Articles