Template type deduction with a non-copyable class

Posted by Evan Teran on Stack Overflow See other posts from Stack Overflow or by Evan Teran
Published on 2010-05-15T22:46:50Z Indexed on 2010/05/15 22:50 UTC
Read the original article Hit count: 269

Filed under:
|
|

Suppose I have an autolocker class which looks something like this:

template <T>
class autolocker {
public:
    autolocker(T *l) : lock(l) {
        lock->lock();
    }

    ~autolocker() {
        lock->unlock();
    }
private:
    autolocker(const autolocker&);
    autolocker& operator=(const autolocker&);
private:
    T *lock;
}

Obviously the goal is to be able to use this autolocker with anything that has a lock/unlock method without resorting to virtual functions.

Currently, it's simple enough to use like this:

autolocker<some_lock_t> lock(&my_lock); // my_lock is of type "some_lock_t"

but it is illegal to do:

autolocker lock(&my_lock); // this would be ideal

Is there anyway to get template type deduction to play nice with this (keep in my autolocker is non-copyable). Or is it just easiest to just specify the type?

© Stack Overflow or respective owner

Related posts about c++

Related posts about template