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?