Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there equivalent of <? extends T>, <? super T> in C++?

  1. Is there equivalent of <? extends T>, <? super T> in C++?

  2. Also, does <? extends T>, <? super T> work even if T is an interface in Java?

like image 900
user855 Avatar asked Nov 27 '09 05:11

user855


People also ask

What is the difference between List <? Super T and List <? Extends T?

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.

What does Super t mean?

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 .


1 Answers

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> ]

like image 160
pprzemek Avatar answered Oct 14 '22 01:10

pprzemek