Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2007-12-18 17:43:12


On Dec 18, 2007, at 4:12 PM, Frank Mori Hess wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Tuesday 18 December 2007 13:53 pm, Howard Hinnant wrote:
>> Some of this is C++0X, like the ability to return movable but non-
>> copyable items from factory functions. Is this getting close to what
>> you were wanting to do?
>
> I think what I can do is just put a bunch of overloaded template
> constructors in my wrapper, sort of like
>
> template<typename Mutex>
> class my_mutex_wrapper
> {
> Mutex mut_;
> public:
> my_mutex_wrapper()
> {//...}
>
> template<typename A1>
> my_mutex_wrapper(A1 a1): mut_(a1)
> {//...}
>
> template<typename A1, typename A2>
> my_mutex_wrapper(A1 a1, A2 a2): mut_(a1, a2)
> {//...}
>
> // ...
>
> template<typename A1, typename A2, /*...*/, typename An>
> my_mutex_wrapper(A1 a1, A2 a2, /*...*/, An an): mut_(a1, a2, /
> *...*/, an)
> {//...}
>
> // ...
> };
>
> One of the constructors will probably be useable by the Mutex
> template type,
> and the invalid ones won't instantiated.

Oh, you are going to so love C++0X! :-)

template<typename Mutex>
class my_mutex_wrapper
{
        Mutex mut_;
public:
         template<typename ...Args>
        my_mutex_wrapper(Args&& ...args): mut_(std::forward<Args>(args)...)
        {//...}
};

This does several things for you:

1. This gives you all of your constructors from the default (0 arg)
up to 1000 args, or whatever the compiler limit happens to be (far
more than you could ever use), with only this one constructor.

2. The arguments will be "perfectly forwarded" without copying from
the client of my_mutex_wrapper to your internal Mutex. If the client
passes in a const lvalue, the Mutex constructor will see a const
lvalue. If the client passes in a non-const rvalue, the Mutex will
see a non-const rvalue.

-Howard


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