mercredi 4 juillet 2018

Detect implementation of conversion operator

I want to detect if a class implements the conversion operator for a specific type, e.g.

struct Foo
{
    operator std::map<std::string, std::string> () { ... }
}

I have implemented the following structure that I can used to detect this and use to select template functions to be used.

template<class T, class D>
struct can_convert_to
{
private:
    template<typename U> static auto check(U* t) -> decltype( static_cast<D>(*t), std::true_type());
    template<typename U> static std::false_type check(...);
public:
    static constexpr bool value = std::is_same<decltype(check<T>(nullptr)), std::true_type>::value;
};

But, here I just check that I can convert, not that the conversion operator is defined. Is there a better way to do this?

Aucun commentaire:

Enregistrer un commentaire