Sealed C++ class.

Dear Boost community, I thank you for such wonderful library, I can not even imagine my life without Boost. Great work, team! I propose to add the small but sometimes very useful class (like boost::noncopyable) I developed it in hope it will be useful and included into the best C++ framework in the world. Note that I have tested this class only with: 1) Microsoft Visual C++ .NET 55537-640-4008961-18142 2) Microsoft Visual C++ .NET 69462-112-0617214-18401 But I think it won't be a problem to compile with gcc and so one. And, surely, sorry if there is already such class in the Boost, I did not found. If it is, please give me the link, I will take a look. Please review and let me know what you think by replying to this message. Thank you very much in advance. // // Copyright (c) 2006 Vladislav Lazarenko <snail@b2bits.com> // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // #ifndef BOOST_SEALED_HPP #define BOOST_SEALED_HPP // MS compatible compilers support #pragma once. #if defined(_MSC_VER) && (_MSC_VER >= 1020) # pragma once #endif namespace boost { // protection from unintended ADL namespace sealed_ { template <typename T1, typename T2> class sealed_impl { friend typename T1; friend typename T2; private: sealed_impl() {} ~sealed_impl() {} }; } /*! The sealed classes is used to prevent derivation from a class. An error occurs if a sealed class is specified as the base class of another class. A sealed class cannot also be an abstract class. */ template <typename T> class sealed : virtual private sealed_::sealed_impl<sealed, T> { public: //! Constructor. sealed() {} //! Destructor. ~sealed() {} }; // Usage example: /* class UniquePerson : public boost::sealed<UniquePerson> { public: UniquePerson() {} ~UniquePerson() {} }; */ } #endif

Vladislav Lazarenko wrote:
template <typename T> class sealed : virtual private sealed_::sealed_impl<sealed, T>
Interesting idea. Any particular reason why this inheritance is virtual? sealed_impl being a template should be rare to duplicate as a base, and having no data members or virtual functions, it should be irrelevant even if it was duplicated. On the other hand, you introduce the troubles that come with virtual inheritance. Sebastian Redl

Sebastian Redl wrote:
Interesting idea. Any particular reason why this inheritance is virtual? sealed_impl being a template should be rare to duplicate as a base, and having no data members or virtual functions, it should be irrelevant even if it was duplicated. On the other hand, you introduce the troubles that come with virtual inheritance.
Sebastian Redl
Dear Sebastian Redl, thank you for your interest. Template class "sealed" inherits "sealed_impl" by virtual to require all classes that will inherit from class "sealed" to call the constructor of "sealed_impl". All classes in the hierarchy are required to call "sealed_impl" constructor, but it is allowed only for two classes, the "sealed" (it was made just to keep code pretty) and that class who will inherit from "sealed". If you will try to inherit from class that already inherits "sealed", you will get error on compilation. It is impossible to implement this thing without virtual inheritance and friend class. I know that virtual inheritance may cause problems in generally, but I see no possible problems with this code. Any suggestions and other solutions are welcome! Thank you.

I thought I saw this before, so I looked it up and thought I would reply with a link to where I saw it. I'll leave it up to you to decide whether this contributes at all to the discussion. http://www.research.att.com/~bs/bs_faq2.html#no-derivation

Arturo_Cuebas@bpmicro.com wrote:
I thought I saw this before, so I looked it up and thought I would reply with a link to where I saw it. I'll leave it up to you to decide whether this contributes at all to the discussion. http://www.research.att.com/~bs/bs_faq2.html#no-derivation
Great article, thank you. As written there - "Unfortunately, that solution is not pretty.". The solution provided there is really not so pretty, but now we have pretty one.

Vladislav Lazarenko wrote:
template <typename T1, typename T2> class sealed_impl { friend typename T1;
This is invalid code. See http://www.comeaucomputing.com/techtalk/templates/#friendclassT Regards, m Send instant messages to your online friends http://au.messenger.yahoo.com
participants (4)
-
Arturo_Cuebas@bpmicro.com
-
Martin Wille
-
Sebastian Redl
-
Vladislav Lazarenko