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 connotのサポートが必要ですコンパイルしてください( "内部エラー"でクラッシュします)。
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);
}
しかし、私はマクロの内部に「現在のタイプを」知っている必要があります:
は実は、私はちょうどoperator==
などの自動生成機能にいくつかのプリプロセッサマクロを書きたいです。
理由だけではなく、代わりに(foobarに、x、y)はマクロGEN_COMPARE_FUNCを作ってみませんか? 'Variadic Macros 'を介して@ForEveR。 – Rollie
それはGCCとMSVCによってサポートされているので、それで十分です。 – qehgt
テンプレート化された 'bool演算子=='を使うことで、誤って2つの型を比較することができます。 –