jeudi 27 avril 2017

c++11 use behavior is statically asserted template type

I have a templated class where I ensure the type parameter is a subclass of some abstract base class like this:

#include <type_traits>

template<typename T>
class Foo
{
    static_assert(
        std::is_base_of<MyBase, T>::value, 
        "T must be a descendant of MyBase"
    );

    void SomeMethod() 
    {
        auto bar = new T();
        bar.someFunctionOnMyBase();
    }
};

Now in programming languages like C# or Java I can use this information and use he type information to call methods on the template type. Is such thing possible? Bonus points if it also possible to call constructors with the correct parameters.

Aucun commentaire:

Enregistrer un commentaire