It's varying!
Functions of Variants are Covariant
by Alfredo Correa
From the article:
Sum types have a range of values that is the sum of the ranges of its parts. std::variant is the model representation of sum types in C++.
For example std::variant can hold an integer value (int state) or a double value (double state). The use of variant types provides support for polymorphism while maintaining value semantics.
There are only a few intrinsic functions that can be applied directly to an std::variant instance in C++; basically, only functions that probe or extract their current type state and value. Simple C++ functions over its component states cannot be applied directly to the variant since the type information needs to be probed before calling the corresponding function over the correct type.
Specific C++ functions can be applied through visitors. However, standard visitors are static and non-covariant, stopping polymorphism from propagating through function application.
A basic explanation of variants and their visitors can be found here.
Add a Comment
Comments are closed.