Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the exact terminology for what you need is "template covariance", meaning that if B inherits from A, then somehow <code>T&lt;B&gt;</code> inherits from <code>T&lt;A&gt;</code>. This is not the case in C++, nor it is with Java and C# generics*.</p> <p>There is a good reason to avoid template covariance: this will simply remove all type safety in the template class. Let me explain with the following example:</p> <pre><code>//Assume the following class hierarchy class Fruit {...}; class Apple : public Fruit {...}; class Orange : public Fruit {...}; //Now I will use these types to instantiate a class template, namely std::vector int main() { std::vector&lt;Apple&gt; apple_vec; apple_vec.push_back(Apple()); //no problem here //If templates were covariant, the following would be legal std::vector&lt;Fruit&gt; &amp; fruit_vec = apple_vec; //push_back would expect a Fruit, so I could pass it an Orange fruit_vec.push_back(Orange()); //Oh no! I just added an orange in my apple basket! } </code></pre> <p>Consequently, you should consider <code>T&lt;A&gt;</code> and <code>T&lt;B&gt;</code> as completely unrelated types, regardless of the relation between A and B.</p> <p>So how could you solve the issue you're facing? In Java and C#, you could use respectively <em>bounded wildcards</em> and <em>constraints</em>:</p> <pre><code>//Java code Bar(Container&lt;? extends Interface) {...} //C# code Bar&lt;T&gt;(Container&lt;T&gt; container) where T : Interface {...} </code></pre> <p>The next C++ Standard (known as C++1x (formerly C++0x)) initially contained an even more powerful mechanism named <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1758.pdf" rel="nofollow noreferrer">Concepts</a>, that would have let developers enforce syntaxic and/or semantic requirements on template parameters, but was unfortunately postponed to a later date. However, Boost has a <a href="http://www.boost.org/doc/libs/1_42_0/libs/concept_check/concept_check.htm" rel="nofollow noreferrer">Concept Check library</a> that may interest you.</p> <p>Nevertheless, concepts might be a little overkill for the problem you encounter, an using a simple static assert as proposed by <a href="https://stackoverflow.com/questions/2203388/c-templates-polymorphism/2203439#2203439">@gf</a> is probably the best solution.</p> <p><sub>* Update: Since .Net Framework 4, it is possible to mark generic parameters has being <a href="http://msdn.microsoft.com/en-us/library/dd799517.aspx" rel="nofollow noreferrer">covariant or contravariant</a>.</sub></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload