Is there a way, using templates, to prevent a class from being derivable in C++

boost, c++

Solution

Under the current spec, it is explicitly forbidden to "friend" a template argument, so templatizing your example would make it not standards compliant. Boost probably would not want to add something like that to its libraries. I believe this restriction is being relaxed in Ox however, and there are workarounds for compilers.

Problem

I need to prevent a class from being derived from so I thought to myself, this is something that Boost is bound to have already done. I know they have a noncopyable, they must have a nonderivable... Imagine my surprise when I couldn't find it.... That got me thinking.. There must be a reason. Maybe it isn't possible to do using templates.. I'm sure if it was easy it's be in the boost libraries. I know how to do it without using templates, i.e. using a base class with a private constructor i.e. ``` class ThatCantBeDerived; // Forward reference class _NonDeriv { _NonDeriv() {} friend class ThatCantBeDerived; }; class ThatCantBeDerived : virtual public _NonDeriv { public: ThatCantBeDerived() : _NonDeriv() { } }; ``` Or something like this.. Maybe it's the forward reference that causes the problem, or maybe there isn't a portable way to achieve it.. Either way, I'm not sure why it isn't in boost.. Any ideas?

Original source