Honestly I'm a big fan of macros, sometimes I even unnecessarily overuse them.
I either use it to add some reflection or I substitute common code snippets to make it easier for me when coding.
There is one particular macro I used to do, but then found out it's unpractical and also quite useless for me since I've left OOP for now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | #define CLASS(p) class p\
{\
inline virtual std::string _GetClassName_() { return #p; }
//which then could be used as
CLASS(TestClass)
void DoStuff();
//...
};
//due to missing brace in code, it broke some VS functionalities and highlighting (which breaks on everything now, excuse me for highlighting).
//I've got rid of this macro and added a factory (which is not related, see below), vector holding pair of string (class name) and class constructor.
//but tbh, I still use simplified version of CLASS macro for ex. as:
class BigDoor : Door
{
public:
REGCLASS("BigDoor") // inline virtual std::string __GetClassName__() { return "BigDoor"; }
//...
};
// and so on..
|
When it comes to macros, I do silly ones often, then there's common stuff like:
| #define SCALL(x, c) if (x != nullptr) c
//or
#include <stdio.h>
#define ASSERT(p,q) if(!p) { fprintf(stderr, "ERROR: \"" ## q ## "\" IN " __FILE__ " AT LINE %d!\n", __LINE__); *(int*)0=0; }
|
Which is pretty self-explanatory and maybe useless, I guess.
I'd like to see some interesting tricks, there's always a time to learn something new.