1
G ++ 버그를 발견했는데 확실하지 않습니다. 나는 그것을 설명 할 수 없다. 컴파일은 BAD 코드를 전달해서는 안되지만 잘못되었습니다. g ++ - 4.5 및 g ++ 4.6 -std = C++ 0x 경고없이이 코드를 전달합니다.G ++ 잘못된 STL 코드를 컴파일
마찬가지로 컴파일은 Bar 객체에 대한 포인터가 Bar 객체 자체라고 생각합니다. 나는 미쳤다. 나는 벌레를 얻기 위해 많은 시간을 보냈다. 이런 종류의 버그로부터 보호 할 수있는 기술이 있습니까?
나쁜 코드를 제공합니다
g++-4.6 for_stackoverflow.cpp && ./a.out
address of bar in main() 0xbff18fc0
Foo 0x9e80008 Bar 0xbff18fec
Foo 0x9e80028 Bar 0xbff18fec
Foo 0x9e80048 Bar 0xbff18fec
end
소스 코드 :
#include <iostream>
#include <list>
#include <iomanip>
#include <algorithm>
#define BAD
using namespace std;
class Bar;
class Foo {
public:
virtual void tick(Bar & b) {
cout << "Foo " << this << " Bar " << setw(14) << (&b) << endl;
}
};
class Bar : public list<Foo*> {
};
int main() {
Bar bar;
cout << "address of bar in main() " << &bar << endl;
bar.push_back(new Foo());
bar.push_back(new Foo());
bar.push_back(new Foo());
#ifdef GOOD
for_each(bar.begin(), bar.end(), bind2nd(mem_fun(&Foo::tick), bar));
#elif defined(BAD)
for_each(bar.begin(), bar.end(), bind2nd(mem_fun(&Foo::tick), &bar));
#else
#error "define GOOD xor BAD"
#endif
cout << "end" << endl;
return 0;
}
bind2nd의 인수 유형이 실제 인수의 유형에서 추론 된 다음 점검 및 관리없이 급격히 변환됩니다. 이것은 다음과 같은 진정한 함정입니다 : (. char x; float f; f = * ((float *) (void *)) &x); –
@ DaneelS.Yaitskov - 표준에서' 'static_cast (x)'대신''std :: bind1st'' /'std :: bind2nd'가 사용되지 않을 가능성이있는 이유 중 하나 인 것 같습니다. 'in C++ 11 –
Flexo