Boost logo

Boost :

From: David B. Held (dheld_at_[hidden])
Date: 2003-03-18 11:36:43


After thinking about boost::ref and the auto_ptr by_ref trick, I
wondered if these could be applied to the function forwarding
problem, like so:

template <typename R, typename T1, typename T2, ...>
R f(by_ref<T1> v1, by_ref<T2> v2, ...)
{
    g(v1, v2, ...);
}

template <typename T>
class by_ref
{
    // ... like boost::reference_wrapper
};

template <>
class by_ref<int>
{
    // value-semantics specializations for fundamental types
};

template <>
class by_ref<double>
{
    // value-semantics specializations for fundamental types
};

template <typename T>
class by_ref<T*>
{
    // ...
};

// etc.

So the default would be to pass by reference, and provide
specializations for known types that should be passed by value,
and let the user specify other types that are pass-by-value.
Another alternative would be go by size, maybe like so:

template <typename T, bool Big = sizeof(T) > 8>
class by_ref
{
    // reference semantics
};

template <typename T>
class by_ref<T, false>
{
    // value semantics
};

Is this is a worthwhile idea to pursue? Am I missing any critical
details? I realize there is still a problem with const vs. non-const
references, but I won't even try to solve that. ;)

Dave


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