Это баг или может нужны опции компилятору?
5.2.7.9 драфта на с++0х:
The value of a failed cast to pointer type is the null pointer value of the required result type.
http://www.kuzbass.ru:8086/docs/isocpp/expr.htm
The value of a failed cast to pointer type is the null pointer value of the required result type.
$ cat test1.cxx
#include <iostream>
#define AS(T) as<T>()
template<typename T> class Boxed;
class Something
{
public:
virtual void print_value_size() {};
template<typename T> T* as() { return (dynamic_cast<Boxed<T>*>(this))->get(); }
virtual ~Something(){}
};
template<typename T> class Boxed: public Something
{
private:
T value;
public:
Boxed<T>(T arg): value(arg) {}
T* get() { return &value; }
virtual void print_value_size() { std::cout << " sizeof=" << sizeof(value); }
virtual ~Boxed(){}
};
template<typename T> inline Boxed<T>* boxed(T arg) { return new Boxed<T>(arg); }
/// usage:
int main()
{
Something* x[]={ boxed(1), boxed("asdf"), boxed(3.0) };
for( int i=0; i<3; i++) {
x[i]->print_value_size();
int* y = x[i]->AS(int);
if( y!=NULL ) std::cout << " int=" << y << ' ';
std::cout << '\n';
}
}
$ g++ test1.cxx && ./a.out
sizeof=4 int=0x804b00c
sizeof=4 int=0x4
sizeof=8 int=0x4
Такая фигня на g++ Debian 4.3.2-1.1 и 4.1.2-21