struct
内部で「現在のタイプ」を取得することは可能struct
ですか? たとえば、次のようなことをしたい:
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;
}
}
私はこのようにしようとしました:
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;
}
}
しかし、見栄えが悪く、最新の C++ 標準のサポートが必要であり、MSVC はそれをコンパイルできません (「内部エラー」でクラッシュします)。
実際には、次のような関数を自動生成するプリプロセッサ マクロをいくつか書きたいだけです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);
}
しかし、マクロ内の「現在の型」を知る必要があります。