Is there equivalent of <? extends T>
, <? super T>
in C++?
Also, does <? extends T>
, <? super T>
work even if T
is an interface in Java?
extends Number> represents a list of Number or its sub-types such as Integer and Double. Lower Bounded Wildcards: List<? super Integer> represents a list of Integer or its super-types Number and Object.
super T denotes an unknown type that is a supertype of T (or T itself; remember that the supertype relation is reflexive). It is the dual of the bounded wildcards we've been using, where we use ? extends T to denote an unknown type that is a subtype of T .
It doesn't have quite nice syntactic sugar as in Java but it's manageable well with boost/type_traits . See http://www.boost.org/doc/libs/1_40_0/libs/type_traits/doc/html/index.html for more info.
#include <boost/type_traits.hpp>
#include <boost/static_assert.hpp>
class Base {};
class Derived_from_Base : public Base {};
class Not_derived_from_Base {};
template<typename BASE, typename DERIVED>
void workOnBase()
{
BOOST_STATIC_ASSERT((boost::is_base_of<BASE, DERIVED>::value));
}
int main()
{
workOnBase<Base, Derived_from_Base>(); // OK
workOnBase<Base, Not_derived_from_Base>(); // FAIL
return 0;
}
1>d:...\main.cpp(11) : error C2027: use of undefined type 'boost::STATIC_ASSERTION_FAILURE' 1> with 1> [ 1> x=false 1> ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With