0

The following code is very basic and obvious:

void PrintParameter(int i, char c)
{
    std::cout << i << std::endl;
}

If I do this:

PrintParameter(5, 'A');

I get 5 as expected and because I don't use the parameter c no error occurs.

Now I think in a weird thing I expect as being an error:

void PrintParameter(int i, char) // declare the type 'char' without the variable
{
    std::cout << i << std::endl;
}

I expected an error because of the absence of the variable of type char.

However, instead of an error, I get 5 as before!

This is a bug of my compiler or a compliance with ISO C++ standard?

In case of not being an error this applies only to C++ or to C also?

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
user7140484
  • 920
  • 6
  • 14
  • 3
    This is in perfect compliance with C++. – Sam Varshavchik Dec 03 '16 at 20:12
  • 1
    @SamVarshavchik but HOW explain this? – user7140484 Dec 03 '16 at 20:13
  • Actually, G++ with style recommendations allowed (`-Weff-c++`, I think) warns you if you *don't* omit the unused parameter. – The Vee Dec 03 '16 at 20:13
  • It's an unnamed parameter, it's perfectly fine to have them. They're useful to explicitly state that you are not using an argument of the method, without having any warnings about it. – Jack Dec 03 '16 at 20:14
  • Are there any situations where you'd _want_ to do this? Ah, yes. When forward declaring something without variable names. – byxor Dec 03 '16 at 20:15
  • It's different between C and C++, illegal in the former. Relevant citations [here](http://stackoverflow.com/a/8776886/1537925). – The Vee Dec 03 '16 at 20:15
  • 1
    @BrandonIbbotson: yes: implementing a virtual method, but not using one of the parameters because implementation does not require it (bad design? maybe) – Jean-François Fabre Dec 03 '16 at 20:15
  • @user7140484 does *what* apply to C? It is not C code. And I am pretty sure you have not found a compiler bug. – Weather Vane Dec 03 '16 at 20:16
  • 1
    There are plenty of situations where you want to do this in C++. Specifically in situations that involve overloading or specialization, where an overloaded operator or a specialized function/class method does not need to use a particular parameter. – Sam Varshavchik Dec 03 '16 at 20:16
  • @BrandonIbbotson, I don't want to use this anywhere, because I don't even know this is possible. I just try to catch a bug and I get a surprise. – user7140484 Dec 03 '16 at 20:18
  • @WeatherVane, ok, just replace the `cout`s with `printf`s – user7140484 Dec 03 '16 at 20:21
  • @user7140484 with `printf(i);`? ); Deleted C tag. – Weather Vane Dec 03 '16 at 20:22
  • @WeatherVane, `printf("%d", i)` as you know – user7140484 Dec 03 '16 at 20:23
  • @user7140484 MSVC compiling a C source says of `void PrintParameter(int i, char)` *"error C2055: expected formal parameter list, not a type list"* as you could have found out yourself ;) – Weather Vane Dec 03 '16 at 20:30
  • @I'm using a C++ compiler and I don't know if C++ compilers behave exactly as C compilers when in presence of plain C code. – user7140484 Dec 03 '16 at 20:33
  • @WeatherVane, sorry I don't read your full answer before. – user7140484 Dec 03 '16 at 20:36

0 Answers0