samedi 30 mai 2015

Delegate to another object's arrow operator

Writing at alias template that will deduce the return type of some type T's operator->, so far I have this

template <typename T>
using arrow = decltype(std::declval<T&>().operator->());

which works for all class types, but doesn't work for pointers. A similar problem exists trying to actually call the ->

template <typename T>
struct D {
    T t;
    arrow<T> f() { 
        // not valid to call .operator->() on pointers
        return t.operator->();
    }
};

How can I make this function get the correct return type declared, and delegate correctly for both class types and pointers?

Aucun commentaire:

Enregistrer un commentaire