Re: Using std::pair<T1,T2> with ABC interfaces
On 26 Nov., 01:00, Andrey Vul wrote:
[...] Is there a workaround (apart from
using pointer-to-interface everywhere, which feels like using the C
sledgehammer) so that ABCs can be used in std::pair<,> ?
Not really. You need a pointer *somewhere*. There are a couple of
options and the right one depends on what you're actually trying to
do:
- raw pointer
- shared_ptr (Boost or TR1 or C++0x)
- unique_ptr (C++0x)
- some kind of "value semantics"-emulating wrapper
(similar to a unique_ptr but calls Base::clone on copy)
template<class Base>
class polyvalue
{
public:
/// takes ownership, will call delete on the pointer
explicit polyvalue(Base* p=0)
: ptr_(p)
{}
/// creates a new copy via the clone member function
polyvalue(polyvalue const& x)
: ptr_(x.ptr_ ? x.ptr_->clone() : 0)
{}
~polyvalue()
{ delete ptr_; }
void swap(polyvalue & that)
{ std::swap(this->ptr_,that.ptr_); }
friend void swap(polyvalue & a, polyvalue & b)
{ a.swap(b); }
polyvalue& operator=(polyvalue tmp)
{ tmp.swap(*this); return *this; }
Base const& operator*() const { return ptr_; }
Base & operator*() { return ptr_; }
Base const* operator->() const { return ptr_; }
Base * operator->() { return ptr_; }
Base const* get() const { return ptr_; }
Base * get() { return ptr_; }
private:
Base* ptr_;
};
(untested)
Cheers!
SG