检测转换运算符的实现
Detect implementation of conversion operator
我想检测 class 是否实现了特定类型的转换运算符,例如
struct Foo
{
operator std::map<std::string, std::string> () { ... }
}
我已经实现了以下结构,我可以用它来检测这一点并用于 select 要使用的模板函数。
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.
那你为什么不检查运算符的存在?
template <typename U>
static auto check (U * t)
-> decltype( t->operator D(), std::true_type{} );
题外话建议:而不是
static constexpr bool value
= std::is_same<decltype(check<T>(nullptr)), std::true_type>::value;
你可以简单地写
static constexpr bool value = decltype(check<T>(nullptr))::value;
下面是一个完整的编译示例
#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, "!" );
}
我想检测 class 是否实现了特定类型的转换运算符,例如
struct Foo
{
operator std::map<std::string, std::string> () { ... }
}
我已经实现了以下结构,我可以用它来检测这一点并用于 select 要使用的模板函数。
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.
那你为什么不检查运算符的存在?
template <typename U>
static auto check (U * t)
-> decltype( t->operator D(), std::true_type{} );
题外话建议:而不是
static constexpr bool value
= std::is_same<decltype(check<T>(nullptr)), std::true_type>::value;
你可以简单地写
static constexpr bool value = decltype(check<T>(nullptr))::value;
下面是一个完整的编译示例
#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, "!" );
}