I have some macros that need access to the type of the current class and I currently get away with this via a DRY-violating pattern:
struct ThisScruct{
int a;
double b;
//example static method using this - purely example - not full usecase
static size_t sum_offsets(){
typedef ThisStruct SelfT;
return offsetof(SelfT, a) + offsetof(SelfT, b);
}
};
This comes up a lot with use of the offsetof
keyword, at least in my own work.
Now before you lock onto this
not being accessible via a static method - realize I just want to know how to get the type ThisStruct
in a generic/macro friendly way from a static method context. I don't actually need/want an instance and am looking for way that actually works like the above without typedeffing SelfT
.
Edit: Something similar is asked in Can I implement an autonomous self
member type in C++? - but I am worried about a diamond problem forming with classes both inheriting from the accepted answer's Self
class.