Posted on 2008-12-05 20:27
chefZ 阅读(366)
评论(0) 编辑 收藏 引用
// checked_cast - Uses fast static_cast in Release build,
// but checks cast with an ASSERT in Debug.
//
// Typical usage:
// class Foo { /* ... */ };
// class Bar : public Foo { /* ... */ };
// Foo * pFoo = new Bar;
// Bar * pBar = checked_cast<Bar *>(pFoo);
template <class TypeTo, class TypeFrom>
TypeTo checked_cast(TypeFrom p)
{
ASSERT(dynamic_cast<TypeTo>(p));
return static_cast<TypeTo>(p);
}