Home > Article > Backend Development > 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
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
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!
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!