-4

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?

Train Heartnet
  • 785
  • 1
  • 12
  • 24
  • 6
    No, it [doesn't](https://godbolt.org/z/6wCLB2) compile actually. – cigien Apr 16 '20 at 16:18
  • 1
    If this compiles, then your compiler is not following the standard. Please state which compiler you are using and with what compilation flags. – walnut Apr 16 '20 at 16:19
  • 1
    I suspect that you're using Microsoft's compiler, which has had this "feature" enabled by default for ages. – molbdnilo Apr 16 '20 at 16:23
  • Visual Studio also fails to compile (2019) – ChrisMM Apr 16 '20 at 16:26
  • @walnut To be super pedantic, successful compilation of ill-formed program is not violation of the standard. Failure to compile a well-formed program, and failure to issue a diagnostic message for an ill-formed program are violations. – eerorika Apr 16 '20 at 16:28
  • @eerorika True of course, my comment was a bit sloppy. – walnut Apr 16 '20 at 16:29
  • I'm running this on MacOS with Apple LLVM version 10.0.1 (clang-1001.0.46.4) with the flags `-std=c++11`,`-Wall`,`-g`, `-pedantic-errors`. Is there any way I can force the compiler to consider it a compile-time error? – Train Heartnet Apr 16 '20 at 16:32
  • 2
    Sorry, but this code still doesn't compile with those settings. [clang 10.0.0 with `-std=c++11 -Wall -Wextra -g -pedantic-errors` fails to compile](https://godbolt.org/z/K3p-ef). Perhaps you've made some other mistake? Are you sure you're trying to compile exactly the code you've shown here? – alter_igel Apr 16 '20 at 16:37
  • For future reference, it would greatly improve your question to include the compiler, its version, and its flags in the question itself. I was ready to assume you were compiling under MSVC, which has [an infamous extension](https://stackoverflow.com/questions/16380966/non-const-reference-bound-to-temporary-visual-studio-bug) that allows this code to compile. – alter_igel Apr 16 '20 at 16:39
  • @TrainHeartnet The `-pedantic-errors` flag is the best you can do to force Clang to produce error messages for programs that are ill-formed according to the standard and as mentioned above Clang does produce an error for the shown program. Please make sure that the code you are showing is completely and exactly the same as you tested. (It certainly is not completely identical because of the missing `#include` and `using` declaration. Please fix that.) – walnut Apr 16 '20 at 16:46
  • @alterigel: Will definitely do that in the future, thanks! Something very weird is happening; for testing this snippet, I had commented the code in an already compiled `.cpp` file, pasted this snippet, and compiled it again, and it compiled just fine. However, when I tried to paste this snippet into an empty file and compile it from scratch, it's throwing the expected error. Very weird behaviour. I'm using VS Code and its C/C++ extension, if that helps. – Train Heartnet Apr 16 '20 at 16:47
  • 1
    @TrainHeartnet If there is other stuff in the file then the answer to this question may change. It may also change for a number of other reasons, e.g. for different function names depending on what you have written before the shown lines. That is why a complete [repro] is so important. – walnut Apr 16 '20 at 16:49

1 Answers1

5

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

They do require it. The shown program is ill-formed.

This code compiles fine.

If the compiler does not issue a diagnostic message, then it does not conform to the C++ standard.

eerorika
  • 232,697
  • 12
  • 197
  • 326