Home > Backend Development > C++ > body text

Why Can\'t I Pass a `Container` to a Constructor Expecting `Container`?

Barbara Streisand
Release: 2024-10-30 08:23:27
Original
276 people have browsed it

Why Can't I Pass a `Container` to a Constructor Expecting `Container`?

Template Polymorphism: When Templates Don't Play Nice

In C , templates provide a powerful mechanism for generic programming. However, there are limitations to their polymorphic behavior. This article answers a question regarding the lack of template polymorphism in a specific scenario.

The Query

A user encounters an "no matching function" error when calling a constructor that takes a parameter of type Container&. The constructor is defined within the Bar class, and the user creates an instance of Container.

Delving into Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common base class. However, this behavior does not extend to templates in C . Despite deriving from Interface, Foo does not automatically make Container a valid argument for a constructor that expects Container.

The Non-Covariance of Templates

The key aspect here is the lack of template covariance. In covariant templates, a derived type is automatically considered a valid argument for a template that expects its base type. However, in C , templates are not covariant.

Consequences of Covariance

Consider the following example:

class Fruit {...};
class Apple : public Fruit {...};
class Orange : public Fruit {...};
std::vector<Apple>& apple_vec; // Vector of apples
std::vector<Fruit>& fruit_vec = apple_vec; // Assignment allowed
fruit_vec.push_back(Orange()); // Oops, added an orange to the apple vector!
Copy after login

As the explanation points out, covariantly treating templates can lead to situations where types that should be incompatible are treated as compatible, breaking type safety.

Alternative Solutions

Since covariant templates are not supported in C , alternative solutions need to be employed. Static asserts or bounded wildcards (available in Java) can be used to enforce the required type relationships. In the specific scenario mentioned in the query, a simple static assert can verify that the passed Container type is indeed a Container.

The above is the detailed content of Why Can\'t I Pass a `Container` to a Constructor Expecting `Container`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!