Is it possible to get "type of the current struct
" inside of the struct
?
For example, I want to do something like this:
struct foobar {
int x, y;
bool operator==(const THIS_TYPE& other) const /* What should I put here instead of THIS_TYPE? */
{
return x==other.x && y==other.y;
}
}
I tried to do it this way:
struct foobar {
int x, y;
template<typename T>
bool operator==(const T& t) const
{
decltype (*this)& other = t; /* We can use `this` here, so we can get "current type"*/
return x==other.x && y==other.y;
}
}
but it looks ugly, requires support of the latest C++ Standard, and MSVC connot compile it (it crashes with "an internal error").
Actually, I just want to write some preprocessor macros to auto-generate functions like operator==
:
struct foobar {
int x, y;
GEN_COMPARE_FUNC(x, y);
}
struct some_info {
double len;
double age;
int rank;
GEN_COMPARE_FUNC(len, age, rank);
}
But I need to know "current type" inside of the macro.