C++ Insights - Implicit Conversions—Andreas Fertig

A good tool!

C++ Insights - Implicit Conversions

by Andreas Fertig

From the article:

This series is motivated by a brief conversation I had with Andreas. I asked him if he has some use case examples which show how C++ Insights can be helpful when teaching. I think there are many things. This article is the start of a series of five posts by Andreas which I will publish at Modernes C++ because I think C++ Insights is an invaluable tool to get a deeper insight in the C++ compiler magic. In case, you are new to C++ Insights consider this introductory article. Without further ado, Andreas post. When you follow the link near to each example, you can directly analyse the example in C++ Insight...

Quick Q: Deletion of copy-ctor & copy-assignment - public, private or protected?

Quick A: The meaning is the same for the compiler, so chooses what make sense to you.

Recently on SO:

Deletion of copy-ctor & copy-assignment - public, private or protected?

I would put them in the public section.

This is because deleting a constructor or an assignment operator is orthogonal to making them private / protected; and when these aren't deleted, they are public. Putting the deletions in one of those two sections seems to me like hinting "If I hadn't deleted them, I would have made them private/protected" - which is not a message you want to convey in your case.

Note, though, that the compiler doesn't care which section you put the deletion in.

Quick Q: Implicit conversion and operator overload

Quick A: it tries to convert to int.

Recently on SO:

Implicit conversion and operator overload

In comparing the conversions needed by different overloaded functions, a "promotion" is considered a better conversion sequence than a standard "conversion". Every arithmetic type can promote to at most one other type. (Promotions are also used when passing an argument to a C-style variadic function like printf. The unary + operator can be used to force a promotion of an arithmetic expression, like +n.)

For integer types which are not character types or bool, the promoted type is:

  • If int can represent all the values of the original type, then int;
  • Otherwise, if unsigned int can represent all the values of the original type, then unsigned int;
  • Otherwise, the original type itself (promotion does nothing)

In your example, when comparing the overloaded functions, an "exact match" would be best, but there is no function taking exactly int8_t (or int8_t& or const int8_t&). The promoted type of uint8_t is int, since it's required to support a range much larger than 0-255. And apparently on your system, int32_t is an alias for int, so the function void f(int32_t); requires only a promotion on the argument. The other functions are all viable, but require an integer conversion on the argument. So void f(int32_t); is considered the best overload.

So the technical answer to the question is that it is implementation specific, but only because of the relationship between int and the <cstdint> types, not because of the overload resolution rules.