c++ - How to test casting time? -
in assignment, have been asked create own static_cast , dynamic_cast using templates specialization. how test static casts done compile-time , dynamic casts on run-time?
template<typename dst, typename src> static dst my_static_cast(src src); template<typename dst, typename src> static dst my_dynamic_cast(src src);
not full answer consider this:
class d { public: virtual ~d(){} }; class : public d{}; class b{};
now dynamic_cast<b*>(new a())
compiles ok (and returns 0) static_cast<b*>(new a())
results in compilation error. i'm sure can sfinae make appropriate test.
of course, test assumes need differentiate between 100% standard-compliant static_cast , 100% standard-compliant dynamic_cast. detecting bugs in implementation additional tests needed.
edit: cannot test automatically. casts require runtime arguments , cannot tested @ compile time. and, @ runtime, difference between compile-time calculated thing , runtime-calculated thing performance. wouldn't recommend testing "well, runtime casts can't fast". isn't reliable.
Comments
Post a Comment