Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can static member functions only be called at global scope if they have a return value?

I found a peculiar thing: static member functions of a class/struct cannot be called a global scope, unless they have a return value.

This program does not compile:

struct test
{
  static void dostuff()
  {
      std::cout << "dostuff was called." << std::endl;
  }
};

test::dostuff();

int main()
{  
   return 0;
}

Giving us the following under GCC v4.8.3:

main.cpp:12:16: error: expected constructor, destructor, or type conversion before ';' token
test::dostuff();
               ^

However, by adding a return value to dostuff() and assigning it to a global variable, the program compiles and works as intended:

struct test
{
  static int dostuff()
  {
      std::cout << "dostuff was called." << std::endl;
      return 0;
  }
};

int i = test::dostuff();

int main()
{
   return 0;
}

This yields the expected output:

dostuff was called.

Can anyone explain to me why this is the case and if there is a work-around that doesn't involve creating global variables?

like image 236
Allan Deutsch Avatar asked May 03 '15 14:05

Allan Deutsch


People also ask

How can a static member can be called in the main function?

How can a static member function be called in the main function? Explanation: The member functions can be called using only the dot operator or the arrow operator. But the static members can be called using directly the class name followed by the scope resolution operator and static member function name.

Can static member function access global functions and data?

They can access global functions and data.

Can we use static variable globally?

A global variable can be accessed from anywhere inside the program while a static variable only has a block scope. So, the benefit of using a static variable as a global variable is that it can be accessed from anywhere inside the program since it is declared globally.

Can static member function private?

Making a function a static member of a class rather than a free function gives two advantages: It gives the function access to private and protected members of any object of the class, if the object is static or is passed to the function; It associates the function with the class in a similar way to a namespace.


1 Answers

You cannot call functions at global scope because, from [basic.link]

A program consists of one or more translation units (Clause 2) linked together. A translation unit consists of a sequence of declarations.

test::dostuff(); is not a declaration - so you cannot have it as a standalone function call (the fact that it's a static member function is irrelevant - could be a free function, a non-static member function called on a global object, etc.). On the other hand, int i = test::dostuff(); is a declaration: of a variable i of type int at global scope. That's why it is allowed. Note that it's not the fact that dostuff() has a return value that is relevant - it's the fact that you're declaring a variable with that return value.

like image 98
Barry Avatar answered Sep 27 '22 22:09

Barry