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
).
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.
And, if it possible, the "how to determine, type From is convertible into a namely reference to type To?"?
Explicit type conversion, also called type casting, is a type conversion which is explicitly defined within a program (instead of being done automatically according to the rules of the language for implicit type conversion). It is defined by the user in the program.
Implicit type conversion is an automatic type conversion done by the compiler whenever data from different types is intermixed. Implicit conversions of scalar built-in types defined in Table 4.1 (except void, double,1 and half 2) are supported.
Use the operator and implicit or explicit keywords to define an implicit or explicit conversion, respectively. The type that defines a conversion must be either a source type or a target type of that conversion. A conversion between two user-defined types can be defined in either of the two types.
What is the need for 'Conversion of data type' in C#? To store a value of one data type into a variable of another data type. To get desired data. To prevent situations of run time error during change or conversion of data type. None of the mentioned.
You need to define your own:
template <class U, class T>
struct is_explicitly_convertible
{
enum {value = std::is_constructible<T, U>::value && !std::is_convertible<U, T>::value};
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With