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:
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 .cpp
file.
edit: Header inclusion in multiple translation units is OK. This example links and runs perfectly fine (built on GCC 4.5):
probe.h:
template <typename T, int N>
struct Probe {
typedef T Type;
};
#define DECLARE_PROBE(type) typedef struct Probe<type, __COUNTER__>
main.cpp:
#include "test.h"
DECLARE_PROBE(int) intprobe;
DECLARE_PROBE(float) floatprobe;
int main(int argc, char** argv) {
intprobe ip;
floatprobe fp;
return 0;
}
test.cpp:
#include "test.h"
DECLARE_PROBE(int) intprobe;
DECLARE_PROBE(float) floatprobe;
static intprobe ip;
static floatprobe fp;