1

In C++, if char to int is implicit conversion and int to char is also implicit conversion. Normally, a char is single or double byte. On 32-bit machine an int is usually 4-byte. Thus, int to char conversion has the possibility of data loss (as moving from bigger area to smaller storage area), where as char to int is more logical candidate for implicit conversion. Now, what about this? Say, we attempt to overload functions with arguments as (int, char) and another as (char, int), if I call the function with (char, char), would this be ambiguous or call to (char, int) is more logical? For example,

void f(char a, int b)
{
    cout << "f(char, int) called" << endl;
}

void f(int a, char b)
{
    cout << "f(int, char) called " << endl;
}

int main()
{
    char a = 'a', b = 'b';
    f(a, b); // which overloaded function should be called?
    return 0;
}
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
  • Please post a [mcve] to illustrate your question; you might also find this will answer your question. – Richard Critten Feb 07 '18 at 12:37
  • 1
    *"Normally, a char is single or double byte"*. No. `char` is never two bytes. It is always exactly one byte. – eerorika Feb 07 '18 at 12:38
  • @user2079303 a char is 1 char - a byte is something else. – Richard Critten Feb 07 '18 at 12:38
  • Neither: `error: call of overloaded 'f(char&, char&)' is ambiguous` – Richard Critten Feb 07 '18 at 12:42
  • @RichardCritten whether they are same or something else does not affect my statement. Size of `char` is 1 byte according to the C++ standard. – eerorika Feb 07 '18 at 12:49
  • 1
    @Richard - Unfortunately the C++ standard has its own definition of the term byte, making it the same size as a `char` - *at least* 8 bits, instead of the more common *exactly* 8 bits. And `sizeof(char) == 1` by that definition. – Bo Persson Feb 07 '18 at 12:53
  • @user2079303 You are (both) right and I was confused. Was miss-remembering that a Byte need not be 8 bits. – Richard Critten Feb 07 '18 at 12:53
  • Why do you think call to `(char, int)` is more logical? there is no `int` to `char` conversion in your example. Do you mean overloading `(char, char)` and `(int, int)` then calling `(char, int)`? – xskxzr Feb 07 '18 at 13:56

2 Answers2

1

If you have two overloads like:

auto foo(int, char) {
    std::cout << __PRETTY_FUNCTION__ << std::endl;
}

auto foo(char, int) {
    std::cout << __PRETTY_FUNCTION__ << std::endl;
}

And then try to call:

foo('a', 'a');

then the call is ambiguous.

For instance, gcc reports that:

error: call of overloaded 'foo(char, char)' is ambiguous
     foo('a', 'a');
                 ^
note: candidate: 'auto foo(int, char)'
 auto foo(int, char) {
      ^~~
note: candidate: 'auto foo(char, int)'
 auto foo(char, int) {
      ^~~

And it makes a perfect sense: the compiler might either promote first char argument to int and call the first overload. Or it might do the same for the second argument. Both ways are equally valid. So the compiler complains.

If you want to call a concrete version, you might use static_cast:

static_cast<void(*)(int, char)>(foo)('a', 'a');
Edgar Rokjān
  • 17,245
  • 4
  • 40
  • 67
0

Now, what about this? Say, we attempt to overload functions with arguments as (int, char) and another as (char, int), if I call the function with (char, char), would this be ambiguous

Yes, it would be ambiguous. Both candidate conversion sequences consist of same number of equally preferred conversion steps.

eerorika
  • 232,697
  • 12
  • 197
  • 326