Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if type can be explicitly converted

Tags:

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?"?

like image 960
Tomilov Anatoliy Avatar asked Jun 03 '13 09:06

Tomilov Anatoliy


People also ask

What is explicit typecasting?

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.

What is implicit type conversion?

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.

Which operator can be used to convert in C#?

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#?

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.


1 Answers

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};
};
like image 133
Andrzej Avatar answered Sep 21 '22 13:09

Andrzej