I have this class:
template <typename T, uint64_t N>
struct Probe {
static const uint64_t Counter = N;
typedef T Type;
};
Which I utilize as:
typedef Probe <int, 0> FirstIntProbe;
typedef Probe <int, 1> SecondIntProbe;
typedef Probe <float, 2> FloatProbe;
Is it possible to create a compile time\macro method which allows me to instantiate this class without specifying the second parameter such as:
typedef Probe <int, Something?> FirstIntProbe;
typedef Probe <int, Something?> SecondIntProbe;
typedef Probe <float, Something?> FloatProbe;
I assume this is isn’t possible, but then again I’ve seen people do things in C++ I wouldn’t think was possible before.
Update:
- It is not necessary to increase by one, it’s just important that
every probe have it’s own number. - It is not needed to have unique
number across different .cpp files\translation units.
You can look into using the
__COUNTER__macro, which is a compiler extension (but supported on GCC and MSVC, among others). Note that__COUNTER__is unique only per-translation-unit, i.e. per.cppfile.edit: Header inclusion in multiple translation units is OK. This example links and runs perfectly fine (built on GCC 4.5):
probe.h:
main.cpp:
test.cpp: