federico-busato / Modern-CPP-Programming

Modern C++ Programming Course (C++03/11/14/17/20/23/26)

Home Page:https://federico-busato.github.io/Modern-CPP-Programming/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Valid overloaded function call marked as error (6. Basic Concepts V)

binary-manu opened this issue · comments

This page says:

image

but then the call taking a single char literal is marked as an error. However, it does compile.

Of the available overloads of f, only 2 can be called with a single argument, but f(float) implies a conversion, while f(int) uses integral promotions, as listed in the rule bullets. Thus, f(int) is selected since it has higher priority.

#include <iostream>

void f(int a) {
    std::cout << "int here!\n";
}

void f(float b) {
    std::cout << "float here!\n";
}

void f(float b, char c) {
    std::cout << "float and char here!\n";
}

int main() {
    f(0);
    f('a');
    f(2.3f);
    //f(2.3);
    f(2.3, 'a');
}

/*
Output:

int here!
int here!
float here!
float and char here!
*/

thanks, @binary-manu. Definitely a mistake on my side. Here the slide updated with some improvements
image