4

I came across this while programming and forgetting the brackets after the function call, but I only got a warning, which I overlooked:

main.cpp:7:15: warning: expression result unused [-Wunused-value]
                if (i == 0) clearDisplay;
                            ^~~~~~~~~~~~

The same thing happens when just doing

int i = 0;
i;

Why isn't this a compiler error?

#include <iostream>

void clearDisplay(void);

int main(void){
    for(int i = 0; i < 2; i++){
        if (i == 0) clearDisplay;
    }
}

void clearDisplay(void){
    std::cout << "test" << std::endl;
}

EDIT: As opposed to g++ "Calling" function without parenthesis ( not f() but f; ). Why always returns 1? I don't ask why it is implicitly casted to a number but why the compiler doesn't warn me about that like it does with if(i=0) to if(i==0)

Community
  • 1
  • 1
mischnic
  • 61
  • 3
  • 8

3 Answers3

8

You can regard clearDisplay as the address of the function. Crucually, it will have a non-zero numerical value, and a statement consisting of a single variable is grammatically correct.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
4

It's the same reason this is not a compiler error:

for(int i = 0; i < 2; i++){

Look at i++. That's an expression whose result you've completely ignored. But that's perfectly legal. Why? Because you are allowed to ignore the results of expressions.

To be helpful, your compiler warns you when you ignore the results of expressions with no side-effects. But if there was a language rule that it was illegal to ignore expressions with no side-effects, that would require compilers to perfectly tell whether expressions have side-effects or not, and that's basically impossible.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • `i++` is the same as `i = i +1;` so it isn't ignored because it changes the value of i – mischnic Jul 08 '15 at 16:28
  • No, the value of the *expression* `i++` is being ignored. – Bathsheba Jul 08 '15 at 16:29
  • @mischnic In `i = i + 1;`, the result *is* ignored. You could have said `printf("%d", i = i + 1);` but instead you ignored the result of assigning a new value to `i`. (Because all you wanted was the side-effect of evaluating the expression, which was an assignment. You didn't care about the value, so you ignored it.) – David Schwartz Jul 08 '15 at 16:30
  • Oh, but `i++;` does something compared to `i;`. And `i = i + 1` actually returns `i+1`, sets the new value and returns the new value. – mischnic Jul 08 '15 at 16:33
  • 1
    Exactly. So C++ has to let you ignore the results of expressions. We often ignore the results of expressions when what we want are the side-effects of evaluating the expression. But, to be nice, compilers should warn you when they see you ignoring the results of expressions they know have no side-effects, since that appears pointless. – David Schwartz Jul 08 '15 at 16:34
  • 1
    @mischnic a similar usage is calling a function and ignore the return value – phuclv Jul 08 '15 at 17:00
  • @LưuVĩnhPhúc but that actually does something – mischnic Jul 08 '15 at 19:57
  • No, the thing here is `expression;` is a valid statement in C and C++. Regardless of the side effects of the expression, you're discarding the result. `i = i + 1` **is** an expression because it returns a value and you can use it in subsequent expression like `if (i = i + 1)` or `j = i = i + 1`... – phuclv Jul 09 '15 at 00:15
  • @mischnic Do you think the compiler can always tell whether an expression has side-effects or not? (Consider `abs(c);` versus `putc(c);`). If not, how could you have a rule that ignoring the results of an expression is only allowed if the evaluation has side-effects. – David Schwartz Jul 09 '15 at 07:18
  • operator++ is a function without using parenthese(), but this is nature of function call of various operators. A function call should have () to distinguish itself. – Nick Huang Feb 15 '22 at 13:51
2

Here's a nutshell explanation.

In the same way that this expression:

i;

is just referring to the variable i without actually doing anything with it, this expression:

clearDisplay;

simply refers to the clearDisplay function without actually calling it.

Sam Estep
  • 12,974
  • 2
  • 37
  • 75
  • Why can I refer to `i` without doing anything? – mischnic Jul 08 '15 at 16:30
  • @mischnic I don't know. I would say it's just another flaw with C++. Other languages like Java don't allow you to do that. – Sam Estep Jul 08 '15 at 16:31
  • Yeah, usually I use java and thats why I stumbled across this. – mischnic Jul 08 '15 at 16:34
  • 1
    Don't forget that some compilers actually produce code when you do this. Try e.g. with gcc (4.8) when `i` is volatile, where you get actual code to load the value of `i` (clang doesn't but warns about the issue). – dhke Jul 08 '15 at 16:39