<p>I've a class which is a wrapper class(serves as a common interface) around another class implementing the functionality required. So my code looks like this.</p>

<pre><code>template&lt;typename ImplemenationClass&gt; class WrapperClass {
// the code goes here
}
</code></pre>

<p>Now, how do I make sure that <code>ImplementationClass</code> can be derived from a set of classes only, similar to java's generics</p>

<pre><code>&lt;? extends BaseClass&gt;
</code></pre>

<p>syntax?</p>

[{'answer_id': 55444, 'author': 'hazzen', 'author_id': 5066, 'author_profile': 'https://Stackoverflow.com/users/5066', 'pm_score': 2, 'selected': False, 'text': '<p>In the current state of things, there is no good way other than by comments or a third-party solution. Boost provides a <a href="http://www.boost.org/doc/libs/1_36_0/libs/concept_check/concept_check.htm" rel="nofollow noreferrer">concept check library</a> for this, and I think gcc also has an implementation. Concepts are on the list of C++0x improvements, but I\'m not sure if you can specify subtypes - they are more for "must support these operations" which is (roughly) equivalent.</p>\n\n<p><strong>Edit:</strong> Wikipedia has this <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Concepts" rel="nofollow noreferrer">section</a> about concepts in C++0x, which is significantly easier to read than draft proposals.</p>\n'}, {'answer_id': 55446, 'author': 'Daniel James', 'author_id': 2434, 'author_profile': 'https://Stackoverflow.com/users/2434', 'pm_score': 4, 'selected': True, 'text': '<p>It\'s verbose, but you can do it like this:</p>\n\n<pre><code>#include &lt;boost/utility/enable_if.hpp&gt;\n#include &lt;boost/type_traits/is_base_of.hpp&gt;\n\nstruct base {};\n\ntemplate &lt;typename ImplementationClass, class Enable = void&gt;\nclass WrapperClass;\n\ntemplate &lt;typename ImplementationClass&gt;\nclass WrapperClass&lt;ImplementationClass,\n      typename boost::enable_if&lt;\n        boost::is_base_of&lt;base,ImplementationClass&gt; &gt;::type&gt;\n{};\n\nstruct derived : base {};\nstruct not_derived {};\n\nint main() {\n    WrapperClass&lt;derived&gt; x;\n\n    // Compile error here:\n    WrapperClass&lt;not_derived&gt; y;\n}\n</code></pre>\n\n<p>This requires a compiler with good support for the standard (most recent compilers should be fine but old versions of Visual C++ won\'t be). For more information, see the <a href="http://www.boost.org/doc/libs/1_36_0/libs/utility/enable_if.html" rel="nofollow noreferrer">Boost.Enable_If documentation</a>.</p>\n\n<p>As Ferruccio said, a simpler but less powerful implementation:</p>\n\n<pre><code>#include &lt;boost/static_assert.hpp&gt;\n#include &lt;boost/type_traits/is_base_of.hpp&gt;\n\nstruct base {};\n\ntemplate &lt;typename ImplementationClass&gt;\nclass WrapperClass\n{\n    BOOST_STATIC_ASSERT((\n        boost::is_base_of&lt;base, ImplementationClass&gt;::value));\n};\n</code></pre>\n'}, {'answer_id': 56551, 'author': 'Douglas Leeder', 'author_id': 3978, 'author_profile': 'https://Stackoverflow.com/users/3978', 'pm_score': 0, 'selected': False, 'text': '<p>See <a href="http://www.research.att.com/~bs/bs_faq2.html#constraints" rel="nofollow noreferrer">Stoustrup\'s own words on the subject</a>.</p>\n\n<p>Basically a small class, that you instantiate somewhere, e.g. the templated classes constructor.</p>\n\n<pre><code>template&lt;class T, class B&gt; struct Derived_from {\n    static void constraints(T* p) { B* pb = p; }\n    Derived_from() { void(*p)(T*) = constraints; }\n};\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/55440', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/108465/']