I tried to make a traits to find if a method is virtual
: (https://ideone.com/9pfaCZ)
// Seveval structs which should fail depending if T::f is virtual or not.
template <typename T> struct Dvf : T { void f() final; };
template <typename T> struct Dvo : T { void f() override; };
template <typename T> struct Dnv : T { void f() = delete; };
template <typename U>
class has_virtual_f
{
private:
template <std::size_t N> struct helper {};
template <typename T>
static std::uint8_t check(helper<sizeof(Dvf<T>)>*);
template<typename T> static std::uint16_t check(...);
public:
static
constexpr bool value = sizeof(check<U>(0)) == sizeof(std::uint8_t);
};
Test cases:
struct V { virtual void f(); };
struct NV { void f(); };
struct E { };
struct F { virtual void f() final; }; // Bonus (unspecified expected output)
static_assert( has_virtual_f< V>::value, "");
static_assert(!has_virtual_f<NV>::value, "");
static_assert(!has_virtual_f< E>::value, "");
But I got error: 'void Dvf<T>::f() [with T = NV]' marked final, but is not virtual
.
If I don't use sizeof
and directly Dvf<T>*
in check
, I don't have compilation error, but check
is not discarded for "bad" type in SFINAE :( .
What is the proper way to detect if a method is virtual
?
There is probably no way to determine if a specific method is
virtual
. I say this because the Boost project researched traits for years and never produced such a traits test.However, in C++11, or using the Boost library, you can use the
is_polymorphic<>
template to test a type to see if the type has virtual functions. See std::is_polymorphic<> or boost::is_polymorphic<> for reference.The code isn't perfect but it basically passes the tests (at least in all clangs available on wandbox and gcc since 7.):
[live demo]
The relevant standard parts that theoretically let the trait fly: [expr.eq]/4.3, [expr.const]/2.23