Reputation: 1064
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?
Upvotes: 1
Views: 190
Reputation: 66210
But, here I just check that I can convert, not that the conversion operator is defined.
So why don't you check the existence of the operator?
template <typename U>
static auto check (U * t)
-> decltype( t->operator D(), std::true_type{} );
Off topic suggestion: instead of
static constexpr bool value
= std::is_same<decltype(check<T>(nullptr)), std::true_type>::value;
you can simply write
static constexpr bool value = decltype(check<T>(nullptr))::value;
The following is a full compiling example
#include <map>
#include <string>
struct Foo
{ operator std::map<std::string, std::string> () { return {}; } };
template <typename T, typename D>
struct can_convert_to
{
private:
template <typename U>
static auto check (U * t)
-> decltype( t->operator D(), std::true_type{} );
template <typename>
static std::false_type check (...);
public:
static constexpr bool value = decltype(check<T>(nullptr))::value;
};
int main ()
{
using mss = std::map<std::string, std::string>;
static_assert( false == can_convert_to<Foo, int>::value, "!" );
static_assert( true == can_convert_to<Foo, mss>::value, "!" );
}
Upvotes: 4