Boost logo

Boost :

Subject: Re: [boost] [type_traits][function_types] Discard param const qualification, bug or feature?
From: Jonathan Wakely (jwakely.boost_at_[hidden])
Date: 2013-09-26 06:40:57


On 26 September 2013 11:02, Andrey Semashev wrote:
>
> Sure, they work the same, from the caller's perspective. But is that a
> reason to cheat with the type system?
>
> void foo(int);
> void bar(const int);
>
> typeid(&foo) == typeid(&bar); // why?
>
> My point was that despite the same behavior on the caller's side, the
> functions have different signatures,

No they don't. "Signature" has a specific meaning in C++ and they have
the same signature.

They have different tokens in the declaration, but so do these:

signed long int i;
long j;

That doesn't mean they have different types.

> and I don't see why there was a
> _necessity_ to force the compiler to drop cv-qualifiers from the function
> argument types. In other words, it makes the language more complicated for
> no apparent reason.

There are reasons.

If they were different signatures would you be able to overload based
on top-level const-ness of parameters? Would that be useful? If the
function takes its arguments by-value why do you (the caller) care
what it does with that parameter? It should be able to modify it or
not, without that affecting the function signature the caller sees.
Whether the parameter is const or not is an internal detail to the
function, not part of the interface.

Currently template argument deduction is constistent, and doesn't
deduce a top-level const. If functions didn't ignore top-level const
on parameters would argument deduction have to change to deduce
top-level const-ness? e.g.

template<typename T>
void f(T by_value) { ++by_value; }

int i;
const int ci;

f(i);
f(ci);

Should that call two different template specializations? Doing so
would break the function template in the f(ci) case because T would be
'const int' and non-modifiable.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk