


Why doesn\'t C support template covariance, and how can we address the resulting type safety issues when working with polymorphic templates?
Oct 28, 2024 am 08:14 AMTemplates and Polymorphism in C
Consider the following class structure:
<code class="cpp">class Interface { // ... }; class Foo : public Interface { // ... }; template <class T> class Container { // ... };</code>
A constructor of some other class Bar is defined as:
<code class="cpp">Bar(const Container<Interface>& bar) { // ... }</code>
However, when attempting to call the constructor as follows:
<code class="cpp">Container<Foo> container(); Bar *temp = new Bar(container);</code>
we encounter a "no matching function" error.
Polymorphism in Templates
The concept of polymorphism in templates, or template covariance, would imply that if class B inherits from class A, then T<B> likewise inherits from T<A>. However, this is not the case in C or other languages like Java or C#.
Reason for Lack of Template Covariance
The absence of template covariance is justified by the need to maintain type safety. Consider the following example:
<code class="cpp">// Class hierarchy class Fruit {...}; class Apple : public Fruit {...}; class Orange : public Fruit {...}; // Template instantiation using std::vector int main() { std::vector<Apple> apple_vec; apple_vec.push_back(Apple()); // Valid // With covariance, the following would be allowed std::vector<Fruit>& fruit_vec = apple_vec; // Adding an Orange to the vector fruit_vec.push_back(Orange()); // Incorrect addition of an orange to an apple vector }</code>
This demonstrates the potential for unsafe behavior if templates were covariant. Therefore, T<A> and T<B> are considered completely distinct types, regardless of the relationship between A and B.
Resolving the Issue
One approach to resolving the issue in Java and C# is to use bounded wildcards and constraints, respectively:
<code class="java">Bar(Container<? extends Interface) {...}
<code class="csharp">Bar<T>(Container<T> container) where T : Interface {...}</code>
In C , the Boost Concept Check library can provide similar functionality. However, using a simple static assert may be a more practical solution for the specific problem encountered:
<code class="cpp">static_assert(std::is_base_of<Interface, Foo>::value, "Container must hold objects of type Interface or its derived classes.");</code>
The above is the detailed content of Why doesn\'t C support template covariance, and how can we address the resulting type safety issues when working with polymorphic templates?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the types of values returned by c language functions? What determines the return value?

C language function format letter case conversion steps

What are the definitions and calling rules of c language functions and what are the

Where is the return value of the c language function stored in memory?

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?

How does the C Standard Template Library (STL) work?
