|
Boost : |
From: Matthew Vogt (mvogt_at_[hidden])
Date: 2004-04-06 17:25:09
Is there any interest in creating a generic base class to encapsulate the
non-derivable class idiom, as recently discussed wrt Reece Dunn's error check
component?
Since the the idiom is on a similar complexity level to that of noncopyable, I
feel it would be appropriate to encapsulate it, if a general solution is
possible.
The following simple base class implements the mechanism as dicussed on
Stroustrup's FAQ page, and encapsulates the virtual inheritance so that the
class can be non-virtually inherited, removing the most obvious chance of
implementation error.
Will this code work on multiple platforms? I have only tested on gcc 3.2...
//-------------------------------
namespace boost {
template <typename T>
struct nonderivable;
namespace detail {
template <typename T>
class nonderivable_base
{
private:
template <typename X>
struct identity { typedef X type; };
friend typename identity<T>::type;
friend class nonderivable<T>;
nonderivable_base() {}
nonderivable_base(const nonderivable_base&) {}
};
} //namespace detail
template <typename T>
struct nonderivable : public virtual detail::nonderivable_base<T>
{
};
} // namespace boost
// ------------------------
struct A : public boost::nonderivable<A> {};
struct B : public A {};
A a; // Ok
B b; // Error - cannot be instantiated
// ------------------------
Matt
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk