On Wed, 25 Sep 2013 02:09:19 -0700, Andrey Semashev
On Wed, Sep 25, 2013 at 12:48 PM, Rob Stewart
wrote: On Sep 25, 2013, at 2:57 AM, Krzysztof Czainski <1czajnik@gmail.com> wrote:
2013/9/25 Mostafa
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.
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