3

How to learn which are conversions and promotions in function calls?.

From my previous question related to why are some functions called despite of others could have also been called too, i want to underline three keywords: conversions, promotions, perfect matches.

  • perfect matches is the simplest:

    • int fun(int a) called with a variable declared int x;
    • int fun(float a, double b) called with a variable declared float x; double y;
    • int fun(char a, string s) called with a variable declared char x; string y
    • ...etc
  • for conversions and promotions i'm only going to mention that:

Numeric conversions: Unlike the promotions, numeric conversions may change the values, with potential loss of precision.

Numeric promotions: a conversion from a smaller type to a larger same type (E.g char to int), but without any loss of content

Here comes the part that is not that simple. I would like someone to explain the way you need to think when you analize the function parameters for different situations as calling:

  • int fun(double a) with float x vs int fun (float a) called with double x

I would like to actual see some examples, because for a begginer is not easy to understand the references from cpp.

Community
  • 1
  • 1
Cătălina Sîrbu
  • 1,253
  • 9
  • 30
  • A better point to start: https://en.cppreference.com/w/cpp/language/overload_resolution – TonySalimi Dec 30 '19 at 16:49
  • i would like to see some examples as `function prototype` and the `variables` along with the function is called. Then a brief explaination of the way you see it (e.g. Here is a conversion because X an Y and there is a promotion because that is an W and a Z) – Cătălina Sîrbu Dec 30 '19 at 16:54

1 Answers1

3

Having:

int fun(double a)

Calling with float x makes this a floating-point promotion, this is because a double will always be the same or a bigger size than a float which means that our parameter a will always be able to hold the data we pass to it through our float, there is no loss of data that can occur and thus it is a promotion.


However, having:

int fun(float a)

Calling with double x makes this a floating-point conversion where loss of data or possible UB can occur. Exactly because this scenario is the opposite of the one above, our double might hold a value not representable in a float which in turn might lead to UB. See Floating-point conversions for the exact rules on that.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
  • @FrançoisAndrieux I thought OP only wanted an example as it seems the question already shows that OP knows what promotion is vs conversion. Conversion being possibly lossy and promotion never being lossy. But maybe I misunderstood. – Hatted Rooster Dec 30 '19 at 17:10
  • I guess you're right that it wasn't asked for, but in trying to answer the question myself I found that the [definition](https://timsong-cpp.github.io/cppwp/conv.fpprom) for floating point promotion doesn't agree with the common understanding that it's about type precision or width. It doesn't look like going from another floating point type to `long double` is ever eligible for being considered a promotion. – François Andrieux Dec 30 '19 at 17:12
  • Would you be kind to explain for example if there is an overloaded function and calling it with a some parameters there could be one promotion or one conversion which one would be chosen? Also could there be any case when it could be a conversion followed by a promotion or viceversa (for the same parameter) at a function call? – Cătălina Sîrbu Dec 31 '19 at 09:03
  • @CătălinaSîrbu Not sure if I understand, can you elaborate? could use a chat room too. – Hatted Rooster Dec 31 '19 at 11:49
  • @CătălinaSîrbu https://chat.stackoverflow.com/rooms/205462/cpp-talk for more info. – Hatted Rooster Jan 06 '20 at 19:02