2

I came across this code the other day:

template< class T > 
T findMax(const T const * data, 
        const size_t const numItems) { 
// Obtain the minimum value for type T 
T largest = 
    std::numeric_limits< T >::min(); 
for(unsigned int i=0; i<numItems; ++i) 
if (data[i] > largest) 
largest = data[i]; 
return largest; 
}

Why do the parameters each contain two consts?

Sir Visto
  • 683
  • 1
  • 4
  • 9
  • 3
    Possible duplicate of [What is the difference between const int\*, const int \* const, and int const \*?](http://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-and-int-const) – clcto Jan 26 '17 at 21:34
  • 2
    I retract my close vote since that is for `const T * const` not `const T const *`. – clcto Jan 26 '17 at 21:36
  • @clcto Still, the answers there teach how to read the `const`s in general. Granted, they may be redundant here. – Gassa Jan 26 '17 at 21:37
  • Unclear if you talk about `data` or about `numItems` but anyway one `const` is redundant with the other for each one.. – Jarod42 Jan 26 '17 at 21:38
  • 3
    It means `error: duplicate 'const'`. – LogicStuff Jan 26 '17 at 21:39
  • 1
    Somebody wanted to be **really, really** sure that `numItems` can't be modified. – Pete Becker Jan 26 '17 at 21:39
  • 2
    Actually g++ produces compilation error – Slava Jan 26 '17 at 21:39
  • I bet this compiles fine by MSVC – Slava Jan 26 '17 at 21:42
  • @Sir Visto "Why do the parameters each contain two consts?" - The reason is single: the author of the code does not know C++ and can not write programs. The function is wrong and does not make sense. – Vlad from Moscow Jan 26 '17 at 22:26

2 Answers2

8

There's no meaningful reason for that. Moreover such explicit repetition of const qualifiers is illegal in C++. The code is ill-formed.

7.1.6.1 The cv-qualifiers [dcl.type.cv]

1 There are two cv-qualifiers, const and volatile. Each cv-qualifier shall appear at most once in a cvqualifier-seq.

It is possible to introduce redundant consts in a C++ declaration, but that would require "hiding" previous consts inside a typedef-name. In that form the declatration would be legal and the redundant qualifiers would be ignored.

Community
  • 1
  • 1
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Actually interesting question comes in mind - does it make any sense to put them both? – Slava Jan 26 '17 at 21:45
  • @Slava: if you mean `const volatile`, yes, it does make sense. Think of e.g. a memory-mapped clock. `const` means you can't write (can't set the clock), `volatile` guarantees (well.. tries to do) that the clock will be read in the appropriate time. – lorro Jan 26 '17 at 21:53
2

One of the const modifiers is redundant; having two here is doing nothing more than one on its own (I'd be surprised if this didn't emit a warning at least).

What the author probably meant was const T* const, which is a constant pointer to a constant instance of T.

OMGtechy
  • 7,935
  • 8
  • 48
  • 83