
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

"Gennadiy Rozental" <gennadiy.rozental@thomson.com> wrote in message news:c4vh07$a38$1@sea.gmane.org...
template <typename T> class nonderivable_base { friend typename identity<T>::type; };
Does is allowed by standard?
Cameau says: "ComeauTest.c", line 8: error: friend class name may not be introduced with "typename" friend typename identity<T>::type; ^ Gennadiy.

Matthew Vogt wrote:
template <typename X> struct identity { typedef X type; };
friend typename identity<T>::type;
Non-standard. Even if you change 'typename' into class/struct, it will still be ill-formed because identity<T>::type resolves to a typedef, which is not allowed. Check with GCC 3.4.0, for instance. -- Giovanni Bajo
participants (3)
-
Gennadiy Rozental
-
Giovanni Bajo
-
Matthew Vogt