Reputation: 190
struct First{
First(){
printf("first");
}
}First;
int main()
{
print("second");
return 0;
}
I know I can control the code that is executed first through the structure.
So I want to use the following code to make it simple through macro.
#define FIRST_INVOKE(NAME,FUNCTION) \
struct NAME \
{ \
NAME() \
{ \
FUNCTION(); \
} \
} NAME;
namespace Foo
{
namespace Bar
{
void FirstFunction()
{
LOGD(LVID, "First");
}
}
}
FIRST_INVOKE(UniqueStructName, Foo::Bar::FirstFunction);
When creating a macro, you must specify the name of the structure. (UniqueStructName)
I want to set this to a name that does not overlap globally automatically.
Namespace and class static functions cannot be included in structure names because :: is required.
Please let me know if there is a good way (I am using Xcode.)
Upvotes: 0
Views: 140
Reputation: 2555
You can automatically generate names based on the line number and use anonymous namespace to avoid multiple definitions across files. E.g, using the following macro:
#define CONCAT_(A,B) A##B
#define CONCAT(A,B) CONCAT_(A,B)
#define NONAME() CONCAT(noname_, __LINE__)
#define FIRST_INVOKE(FUNCTION) \
namespace { \
struct NONAME() \
{ \
NONAME()() \
{ \
FUNCTION(); \
} \
} NONAME(); \
}
This will expand to a noname_X
struct, where X
is the line-number. This works as long as you do not repeat the macro on the same line.
Upvotes: 1