Boost logo

Boost :

Subject: Re: [boost] [type_traits][function_types] Discard param const qualification, bug or feature?
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2013-09-30 22:54:56


On 10/1/2013 2:46 PM, Quoth Mostafa:
> struct SomeUserClass
> {
> static void foo(int const x)
> {
> SomeCodeGenClass::foo(x);
> }
> };
>
> SomeCodeGenClass::foo is a mere parameter forwarder, so the goal is to
> do it as efficiently as possible. It's signature is constructed from
> SomeUserClass::foo. For correctness, that should be:
>
> void SomeCodeGenClass::foo(int const & x)

This would also be the correct signature for foo(int x) -- because
they're the same thing.

> But, function_types<SomeUserClass::foo>::arg1_type resolves to int, so
> that add_reference'ing will give the following signature for the TMP
> constructed SomeCodeGenClass::foo
>
> void SomeCodeGenClass::foo(int & x)

A simple add_reference is obviously the wrong thing to be doing then.

In general, you can take any parameter type T and wrap it as a "const
T&" (or if you prefer, "T const&") and it will do the right thing,
unless T was already a reference. But note that you must use a const
reference -- a non-const reference won't work.

Technically, if you're aiming for "most efficient format" you should
also check whether the size of the type is less than the size of the
reference (which is pointer-sized) and use the original type in that
case. Though this is an optional improvement.

You might also want to read up on "perfect forwarding", although this is
only available in C++11.


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