问题描述
如何(按照< 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};
};
这篇关于检查是否可以显式转换类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!