How to get information about the "current type" inside the structure / class?

Is it possible to get the "type of current struct" inside 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 as follows:

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 for the latest C ++ Standard and compilation of its MSVC compiler (it crashes with an "internal error").

In fact, I just want to write some preprocessor macros to automatically create functions such as 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 the "current type" inside the macro.

+5
source share
2 answers

Actually, you can use something like this.

#define GEN_COMPARE_FUNC(type, x, y)\
template<typename type>\
bool operator ==(const type& t) const\
{\
    return this->x == t.x && this->y == t.y;\
}

struct Foo
{
    int x, y;
    GEN_COMPARE_FUNC(Foo, x, y);
};

, var. macro-pars ( params par t, , ).

0

URL- , boost , C/++ :

- :

++?

typeof, typeof:

#include <boost/typeof/typeof.hpp>

, BOOST_TYPEOF:

namespace ex1
{
    typedef BOOST_TYPEOF(1 + 0.5) type;

    BOOST_STATIC_ASSERT((is_same<type, double>::value));
}
0

All Articles