개인적으로는하지 않지만 고유 한 이름을 사용합니다. '
이
#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
및 for
의 조합을 사용하는 것입니다 갈등. 내부 이름은 외부 이름을 숨 깁니다. if
및 for
루프의 식은 상수이므로 컴파일러에서 쉽게 최적화해야합니다. 당신이 정말로 식을 전달하려는 경우
, 당신은 ScopedGuard 트릭 (Most Important const
참조)를 사용할 수 있지만 그것을 쓰기 위해 몇 가지 더 많은 작업이 필요합니다. 당신에게 지금
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;
};
:
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
기능을 제공 :하지만 좋은 측면은 우리가 for
루프 제거하기, 우리의 목적은 false
으로 평가 할 수있다
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
는 다음 쇼
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;
}
}
모든 이들의 더 가상 함수를 포함하지 같은 scoped_obj<Class>
에서 RAII 객체를 상속해야하고, 관련된 기능은 컴파일러에 투명합니다. 최적화 레벨 -O2
에서 사실
// 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 : 위의 GLTranslate
글로벌 변수에 하나의 정수를 추가로 변경하여 다시 빼서 떠날 때, 그리고 아래 GLTranslateE
정의 사실, 내가 테스트를했다
main:
sub $29, $29, 8
ldw $2, $0, j
add $2, $2, 5
stw $2, $0, j
.L1:
add $29, $29, 8
jr $31
나는 그것을 아주 잘 최적화했다.
내가 볼 수 없습니다 필요합니다. –
그게 무슨 가치가 있는지, 나는 한 번에 비슷한 계획을 시도했다. 나는 당신이 가지고있는 것처럼'push/pop'을 가진'Transformation' 클래스의 형식을 만드는 것이 더 쉽다는 것을 알았습니다. 번역을위한 호출을하는 멤버 함수 등등. 그런 다음 당신은 하나의 클래스만을 가지고 있습니다. 필요할 때. – GManNickG
나는 응답이 __LINE__ 또는 __COUNTER__ :-)이라고 생각한다. – anon