检查是否可以显式转换类型 [英] check if type can be explicitly converted

查看:56
本文介绍了检查是否可以显式转换类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何(按照< type_traits> 精神)确定一种类型是否可以明确转换为另一种类型?例如,我要检查 F :: explicit运算符double const&的存在.()const; 用于某些 class / struct F ,但同时是 F 不应明确地转换为 float long double (类似于 pred< double const&> :: value&&!pred< float> :: value&&!pred< long double> :: value ).

How to determine (in the <type_traits> spirit) whether or not one type explicitly convertible into another type? For example, I want to check the presence of F::explicit operator double const & () const; for some class/struct F, but, at the same time, F should not been explicitly convertible to float or long double (something like pred< double const & >::value && !pred< float >::value && !pred< long double >::value).

请注意, std :: is_convertible<从,到> :: value 检查是否可以使用隐式转换转换为".但我想确定是否存在显式转换运算符.

Note, that std::is_convertible< From, To >::value checks "if From can be converted to To using implicit conversion". But I wish to determine whether there is the explicit conversion operator.

并且,如果可能的话,如何确定类型 From 是否可以转换为引用,以 To 类型?"?

And, if it possible, the "how to determine, type From is convertible into a namely reference to type To?"?

推荐答案

您需要定义自己的:

template <class U, class T>
struct is_explicitly_convertible
{    
  enum {value = std::is_constructible<T, U>::value && !std::is_convertible<U, T>::value};
};

这篇关于检查是否可以显式转换类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆