Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++, calling a function without (), what do that mean?

Tags:

c++

g++

five minutes ago I did something I never did before (it's not about sex...) look at the code :

// g++ DeathToAllButMetal.cc
#include<iostream>

void DeathToAllButMetal(){
  std::cout << "A MASTERPIECE MADE BY STEEL PANTHER" << std::endl;
} 

int main(){
  DeathToAllButMetal();

  DeathToAllButMetal;

  return 0;
}

you see I forgot the "()" at the second DeathToAllButMetal surprisingly the compiler doesn't yell on me. So for the compiler this line of code means something, but what ? because at the running, the method is not called... It's look to be a basic question, but I don't remember having saw this in the past.

like image 863
The Unholy Metal Machine Avatar asked Dec 09 '22 14:12

The Unholy Metal Machine


1 Answers

It's just a no-op. The name of a function decays into a pointer to that function in that context, but since you don't do anything with it, nothing happens. If you turn up your warnings level you may get a message along the lines of "statement has no effect". For example, clang++ says (even with no flags):

example.cpp:11:3: warning: expression result unused [-Wunused-value]
  DeathToAllButMetal;
  ^~~~~~~~~~~~~~~~~~
1 warning generated.

G++ warned too, but I needed to add -Wall:

example.cpp: In function ‘int main()’:
example.cpp:11: warning: statement is a reference, not call, to function ‘DeathToAllButMetal’
example.cpp:11: warning: statement has no effect
like image 71
Carl Norum Avatar answered Dec 28 '22 19:12

Carl Norum