You can check for virtual methods by comparing the type size with the type of the type with the added virtual method. This type of validation is not guaranteed by the standard and can be tricked by virtual inheritance, so it should not be used in production code. However, it can be useful for simple cases where C ++ 11 std::is_polymorphic not available. Tested under g ++ 4.6:
template<typename T> class VirtualTest: private T { virtual void my_secret_virtual(); }; template<typename T> bool has_virtual() { return sizeof(T) == sizeof(VirtualTest<T>); }
Call the test as has_virtual<A>() .
source share