jeudi 16 avril 2020

Why don't char literals require the function's parameter to be a reference to const?

Consider this snippet:

void foo(const string &s, char &c)
{
// do something
}

int main()
{
    foo("Hello World!",'o');
    return 0;
}

This code compiles fine.

When I call foo, since I may pass a string literal as argument for the s parameter, s needs to be const string &, as a string literal cannot be converted to a plain reference. But why isn't this the case for the c parameter? I can pass a char literal just fine, without having c be a reference to const. Why is this?

Aucun commentaire:

Enregistrer un commentaire