3

talking about a function parameter, for example:

void doSomething(const int const &someVal);

As far as I understand, the fist const indicates that the value will not be changed by the function, and the & indicates that the value should be passed by reference. But what does the second 'const' do?

user81993
  • 6,167
  • 6
  • 32
  • 64
  • 6
    It irritates, confuses, and causes [compiler diagnostics](http://ideone.com/ZYakRQ). – Kerrek SB Oct 03 '14 at 18:16
  • 3
    Make the program ill-formed? – David G Oct 03 '14 at 18:16
  • 1
    It's holding the candle. – jrok Oct 03 '14 at 18:17
  • 1
    Soo.. it should not be there? Hey, its not my code, I'm just working off an example. – user81993 Oct 03 '14 at 18:19
  • 1
    The first `const` placement is a syntax nicety provided by the standard when *openning* a def. It *does* what the second `const` is already doing. `int const& someVal` and `const int& someVal` are synonymous. Remove *either* . – WhozCraig Oct 03 '14 at 18:22
  • 1
    @0x499602D2: That a fact? (It's certainly bad) – Deduplicator Oct 03 '14 at 18:23
  • In `const int const *someVal`, both `const`s are meaningful. Perhaps the author mistakenly though that the same was true for references. (References are loosely similar to `const` pointers.) – Keith Thompson Oct 03 '14 at 18:30
  • 1
    @KeithThompson even in that the second `const` is duplicate. If you meant `const int *const someVal;`, synonymous with `int const * const someVal;` then yeah, that holds, so long as the second `const` is *after* the `*`. – WhozCraig Oct 03 '14 at 18:37
  • @Deduplicator GCC reported an error. And now I look at it I see clang gave a warning. – David G Oct 03 '14 at 18:38
  • @WhozCraig: You're right. – Keith Thompson Oct 03 '14 at 18:39
  • @The double-indirection chart in an [answer I wrote a long time ago](http://stackoverflow.com/a/14566215/1322972) somewhat chiseled it in my head. I particularly liked the *last* one in the list. Thought about making T-shirts: "Got `const`?" – WhozCraig Oct 03 '14 at 18:42

2 Answers2

2

According to the C++ Standard (7.1.6.1 The cv-qualifiers)

1 There are two cv-qualifiers, const and volatile. Each cv-qualifier shall appear at most once in a cvqualifier- seq. If a cv-qualifier appears in a decl-specifier-seq, the init-declarator-list of the declaration shall not be empty. [ Note: 3.9.3 and 8.3.5 describe how cv-qualifiers affect object and function types. —end note ] Redundant cv-qualifications are ignored. [ Note: For example, these could be introduced by typedefs.—end note ]

So this declaration

void doSomething(const int const &someVal);

has a redundant const qualifier that is simply ignored.

The second const qualifier would have a sense if someVal would be declared as a reference to const pointer. For example

void doSomething(const int * const &someVal);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • It's not just ignored. `const int const` is ill-formed by 7.1.6 [dcl.type]/p2, first bullet point: "`const` can be combined with any type specifier _except itself_." (emphasis added). – T.C. Oct 03 '14 at 21:16
0

Both of these should work:

void doSomething(const int &someVal);

void doSomething(int const &someVal);

Using a second const doesn't add any functionality and the extra one should be removed. It's a good practice to keep the const to the left though for clarity as it stands out better.

Augustin Popa
  • 1,423
  • 1
  • 19
  • 22