Boost logo

Boost :

Subject: Re: [boost] [type_traits][function_types] Discard param const qualification, bug or feature?
From: Mostafa (mostafa_working_away_at_[hidden])
Date: 2013-09-25 10:59:24


On Wed, 25 Sep 2013 02:09:19 -0700, Andrey Semashev
<andrey.semashev_at_[hidden]> wrote:

> On Wed, Sep 25, 2013 at 12:48 PM, Rob Stewart
> <robertstewart_at_[hidden]>wrote:
>
>> On Sep 25, 2013, at 2:57 AM, Krzysztof Czainski <1czajnik_at_[hidden]>
>> wrote:
>>
>> > 2013/9/25 Mostafa <mostafa_working_away_at_[hidden]>
>> >
>> >> Both the type_traits and function_types library discard parameter
>> const-qualifiers when decomposing function types. Is this a bug or a
>> >> feature? If a feature, then it's surprising enough behaviour that I
>> think it warrants some documentation in both libraries.
>>
>> [snip]
>>
>> >> typedef void (foo)(int const);
>> >
>> > The two function declarations:
>> > void f(int);
>> > void f(int const);
>> > declare the same function, not an overload.
>>
>> Right. Top level const is not part of the function's type.
>>
>> > Const added to an argument type passed by value doesn't change the
>> function signature. It is because the parameter will be copied anyway,
>> and
>> the const refers to how the argument
>> > will be treated inside the function's implementation.
>>
>> Exactly.
>>

Ah, thanks. I was only thinking in terms of the parameter type.

>
> <complain_mode>
>
> I have to say that while these rules are logical and understandable when
> explained, types of function arguments are a constant source of
> confusion.

Exactly. That's why an explanatory note or two in the documentation would
go a long way.

> The described above quirk with const arguments can be considered quite
> rare
> as people usually don't declare arguments that way.

It can show up in some meta-programming code. Say you want to capture a
value by const and let the meta-programmed code forward it to some
destination, and for the sake of efficiency you decided to the forwarding
by reference. If you're not careful, you can end up with an 'int &' to an
'int const' in the meta-programmed code.

Mostafa


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