Boost logo

Boost :

From: dherring_at_[hidden]
Date: 2008-03-26 12:49:58

On Wed, 26 Mar 2008, Dean Michael Berris wrote:
> On Wed, Mar 26, 2008 at 7:11 AM, <dherring_at_[hidden]> wrote:
>> Quick question:
>> In boost/pool/detail/gcd_lcm.hpp, why does
>> template <typename Integer>
>> Integer lcm(const Integer & A, const Integer & B);
>> take its parameters as const references instead of just passing the
>> values?
> This avoids unnecessary copying in case existing 'Integer'
> objects/instances are passed to the function. For example, you can use
> this to make your own 'Integer-conforming' class and avoid extraneous
> copies. In case you pass in values, you cause the creation of
> temporaries -- which the compiler can optimize out later for you.

Can, not will.

I stumbled across this on an older compiler (not supported by Boost);
the linker was failing on unresolved symbols, forcing an explicit

#include <boost/pool/pool.hpp>
namespace boost
     template class pool<boost::default_user_allocator_new_delete>;
     template unsigned int details::pool::gcd<unsigned int>(const unsigned
int, const unsigned int);
     template unsigned int details::pool::lcm<unsigned int>(const unsigned
int &, const unsigned int &);
     template class simple_segregated_storage<unsigned int>;

My question arose since this compiler apparently isn't inlining the
functions. Passing "const unsigned int &" parameters just feels wrong.

- Daniel

Boost list run by bdawes at, gregod at, cpdaniel at, john at