私はこれを個人的にはしませんが、ただユニークな名前を思いつきます。しかし、それを実行したい場合、1つの方法はとの組み合わせを使用することif
ですfor
:
#define FOR_BLOCK(DECL) if(bool _c_ = false) ; else for(DECL;!_c_;_c_=true)
あなたはそれを次のように使うことができます
FOR_BLOCK(GlTranslate t(1.0, 0.0, 0.0)) {
FOR_BLOCK(GlTranslate t(1.0, 1.0, 0.0)) {
...
}
}
これらの名前はそれぞれ別々のスコープにあり、競合することはありません。内側の名前は外側の名前を隠します。if
andループの式for
は定数であり、コンパイラーによって簡単に最適化される必要があります。
本当に式を渡したい場合は、ScopedGuardトリック(「最も重要const
」を参照)を使用できますが、式を作成するにはさらに作業が必要になります。for
しかし、良い面は、ループを取り除き、オブジェクトを次のように評価できることfalse
です。
struct sbase {
operator bool() const { return false; }
};
template<typename T>
struct scont : sbase {
scont(T const& t):t(t), dismiss() {
t.enter();
}
scont(scont const&o):t(o.t), dismiss() {
o.dismiss = true;
}
~scont() { if(!dismiss) t.leave(); }
T t;
mutable bool dismiss;
};
template<typename T>
scont<T> make_scont(T const&t) { return scont<T>(t); }
#define FOR_BLOCK(E) if(sbase const& _b_ = make_scont(E)) ; else
次に、適切な機能を提供しenter
ますleave
。
struct GlTranslate {
GLTranslate(float x, float y, float z)
:x(x),y(y),z(z) { }
void enter() const {
glPushMatrix();
glTranslatef(x, y, z);
}
void leave() const {
glPopMatrix();
}
float x, y, z;
};
これで、ユーザー側に名前を付けずに完全に書き込むことができます。
FOR_BLOCK(GlTranslate(1.0, 0.0, 0.0)) {
FOR_BLOCK(GlTranslate(1.0, 1.0, 0.0)) {
...
}
}
一度に複数の式を渡したい場合は少し注意が必要ですが、operator,
すべての式をに収集するように機能する式テンプレートを作成できますscont
。
template<typename Derived>
struct scoped_obj {
void enter() const { }
void leave() const { }
Derived const& get_obj() const {
return static_cast<Derived const&>(*this);
}
};
template<typename L, typename R> struct collect
: scoped_obj< collect<L, R> > {
L l;
R r;
collect(L const& l, R const& r)
:l(l), r(r) { }
void enter() const { l.enter(); r.enter(); }
void leave() const { r.leave(); l.leave(); }
};
template<typename D1, typename D2>
collect<D1, D2> operator,(scoped_obj<D1> const& l, scoped_obj<D2> const& r) {
return collect<D1, D2>(l.get_obj(), r.get_obj());
}
#define FOR_BLOCK(E) if(sbase const& _b_ = make_scont((E))) ; else
scoped_obj<Class>
次の図のように、RAIIオブジェクトを継承する必要があります
struct GLTranslate : scoped_obj<GLTranslate> {
GLTranslate(float x, float y, float z)
:x(x),y(y),z(z) { }
void enter() const {
std::cout << "entering ("
<< x << " " << y << " " << z << ")"
<< std::endl;
}
void leave() const {
std::cout << "leaving ("
<< x << " " << y << " " << z << ")"
<< std::endl;
}
float x, y, z;
};
int main() {
// if more than one element is passed, wrap them in parentheses
FOR_BLOCK((GLTranslate(10, 20, 30), GLTranslate(40, 50, 60))) {
std::cout << "in block..." << std::endl;
}
}
これらはすべて仮想関数を含まず、関連する関数はコンパイラに対して透過的です。実際、上記GLTranslate
を変更してグローバル変数に単一の整数を追加し、それを再び減算したままにしておくと、以下で定義されているようにGLTranslateE
、テストを実行しました。
// we will change this and see how the compiler reacts.
int j = 0;
// only add, don't subtract again
struct GLTranslateE : scoped_obj< GLTranslateE > {
GLTranslateE(int x):x(x) { }
void enter() const {
j += x;
}
int x;
};
int main() {
FOR_BLOCK((GLTranslate(10), GLTranslateE(5))) {
/* empty */
}
return j;
}
実際、最適化レベルのGCCは-O2
これを出力します。
main:
sub $29, $29, 8
ldw $2, $0, j
add $2, $2, 5
stw $2, $0, j
.L1:
add $29, $29, 8
jr $31
私はそれを予期していなかったでしょう、それはかなりうまく最適化されました!