[pool] boost::details::pool::lcm parameters

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? I don't see a reason to do this; gdc(A, B) takes values. Thanks, Daniel P.S. As seen in boost_1_35_0_RC2, aka boost-posix-2008-03-21.

On Wed, Mar 26, 2008 at 7:11 AM, <dherring@ll.mit.edu> 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. -- Dean Michael C. Berris Software Engineer, Friendster, Inc. [http://blog.cplusplus-soup.com] [mikhailberis@gmail.com] [+63 928 7291459] [+1 408 4049523]

On Wed, 26 Mar 2008, Dean Michael Berris wrote:
On Wed, Mar 26, 2008 at 7:11 AM, <dherring@ll.mit.edu> 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
participants (2)
-
Dean Michael Berris
-
dherring@ll.mit.edu