that this snippet of code actually do?
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
void test();
namespace {
static struct StaticStruct {
StaticStruct() {
test();
}
} TheStaticSupport;
}
int main(void) {
return 0;
}
void test() {
printf("testing function\n");
}
why does the test function actually get called? and why use the “anonymous” namespace? I found this piece of code in an open source project…
This:
Is equivalent to this:
It defines a type named
StaticStructand an instance of the type namedTheStaticSupportwith internal linkage (though, since it is declared in an unnamed namespace, thestaticis redundant).The constructor for
TheStaticSupportis called beforemain()is entered, to construct the object. This calls thetest()function.