A way to create random (actually sequentially numbered) variable names is demonstrated in the question How to generate random variable names in C++ using macros?.
However, it is not explained how such a generated name can be accessed later. Imagine a class declaration with a constructor. You need the same random name for the class name and the c-tor. Always using the generator macro just generates a new identifer and the compilation fails. Here's an example:
#define CONCAT_(x,y) x##y
#define CONCAT(x, y) CONCAT_(x, y)
#define DESCRIBE_IMPL CONCAT(DescribeImpl, __COUNTER__)
class DESCRIBE_IMPL {
public:
DESCRIBE_IMPL() {}
};
I tried storing the current counter value somehow (e.g. enum { COUNTER = __COUNTER__ }
, but using this enum always leads to the name DescribeImplCOUNTER
instead of the variant with the counter value.
The Real Problem
What I'm trying to solve with this approach is a case where I have multiple definitions of the same class in different cpp files (generated from macros) and the linker complains about multiple symbols (ODR violation). I cannot change this approach without significantly change the usage pattern.