Explicit C ++ Function Signatures

I would like to create a helper shell against the type structure of the variant, so that the type signature will cause the necessary conversions, for example:

variant CreateVariant(bool value); variant CreateVariant(int value); variant CreateVariant(char *value); 

The problem is bool and int, as they are implicitly convertible types ...

So, for code like:

 variant a = CreateVariant((BOOL)value); variant a = CreateVariant((__int64)value); variant a = CreateVariant(1); 

There are some problems.

How can I create a clean wrapper if I need to treat bool as boolean and unigned / signed int / uints / longs / longlongs as integer types?

For class constructors, I can use the explicit keyword, but I need to use int functions for the code I'm updating.

+4
source share
1 answer

As follows from the comments, the answer to your question is the difference between "bool" (real Boolean type) and BOOL (MS typedef from int).

In the broader question, however, I did the same in the code. If you are on an MS platform, consider using _variant_t or CComVariant: both do basically what you want. I ended my own conversion class with a template that was passed by default to the base parent class (in this case _variant_t) and added specializations for constructor types that did not have a built-in conversion to the MS class type. You will need to add specializations for each type, for which there is no conversion in the base type, but usually this is not so bad.

I have to add:

The advantage of this method is that there will be no implicit conversion. If the type matches an explicit override, it will be used. Otherwise, it will match the pattern and be passed to the MS type. If the MS type cannot accept the passed type, you will get a compile-time error and know what you need to address.

+1
source

Source: https://habr.com/ru/post/1399916/


All Articles