std :: is_base_of和虚拟基类

时间:2017-08-29 21:50:15

标签: c++ c++11 enable-if

有没有办法确定基类是否是虚拟基类?

std :: is_base_of将识别基类,但我正在寻找像std :: is_virtual_base_of这样的东西来识别虚拟基类。

这适用于SFINAE目的,我想在std :: is_virtual_base_of为true时使用dynamic_cast(性能较低),而在false时使用static_cast(性能更高)。

2 个答案:

答案 0 :(得分:0)

namespace details {
  template<template<class...>class, class, class...>
  struct can_apply:std::false_type {};
  template<class...>struct voider{using type=void;};
  template<class...Ts>using void_t=typename voider<Ts...>::type;
  template<template<class...>class Z, class... Ts>
  struct can_apply<Z, void_t<Z<Ts...>>, Ts...>:std::true_type {};
}
template<template<class...>class Z, class... Ts>
using can_apply = details::can_apply<Z, void, Ts...>;

这使我们可以轻松地在模板应用程序上进行模块化SFINAE。

template<class Dest, class Src>
using static_cast_r = decltype(static_cast<Dest>( std::declval<Src>() ));
template<class Dest, class Src>
using can_static_cast = can_apply< static_cast_r, Dest, Src >;

现在我们可以确定我们是否可以静态演员。

现在我们实施它:

namespace details {
  template<class Dest, class Src>
  Dest derived_cast_impl( std::true_type /* can static cast */ , Src&& src )
  {
    return static_cast<Dest>(std::forward<Src>(src));
  }

  template<class Dest, class Src>
  Dest derived_cast_impl( std::false_type /* can static cast */ , Src&& src )
  {
    return dynamic_cast<Dest>(std::forward<Src>(src));
  }
}
template<class Dest, class Src>
Dest derived_cast( Src&& src ) {
  return details::derived_cast_impl<Dest>( can_static_cast<Dest, Src&&>{}, std::forward<Src>(src) );
}

测试代码:

struct Base { virtual ~Base() {} };

struct A : virtual Base {};
struct B : Base {};

struct Base2 {};
struct B2 : Base2 {};

int main() {
  auto* pa = derived_cast<A*>( (Base*)0 ); // static cast won't work
  (void)pa;
  auto* pb = derived_cast<B*>( (Base*)0 ); // either would work
  (void)pb;
  auto* pb2 = derived_cast<B2*>( (Base2*)0 ); // dynamic cast won't work
  (void)pb2;
}

live example

答案 1 :(得分:0)

使用 c++17 很容易实现。

#include <type_traits>

// First, a type trait to check whether a type can be static_casted to another    
template <typename From, typename To, typename = void>
struct can_static_cast: std::false_type{};

template <typename From, typename To>
struct can_static_cast<From, To, std::void_t<decltype(static_cast<To>(std::declval<From>()))>>: std::true_type{};

// Then, we apply the fact that a virtual base is first and foremost a base,
// that, however, cannot be static_casted to its derived class.
template <typename Base, typename Derived>
struct is_virtual_base_of: std::conjunction<
    std::is_base_of<Base, Derived>, 
    std::negation<can_static_cast<Base*, Derived*>>
>{};

// Proof that it works.
struct Base{};
struct NonVirtual: Base{};
struct Virtual: virtual Base{};

static_assert(is_virtual_base_of<Base, NonVirtual>::value == false);
static_assert(is_virtual_base_of<Base, Virtual>::value == true);

在 Godbolt 上看到它:https://godbolt.org/z/jxjq5W

使用 c++11,它有点不干净。这是:

#include <type_traits>

template <typename From, typename To, typename = void>
struct can_static_cast: std::false_type{};

template <typename From, typename To>
struct can_static_cast<From, To, decltype(static_cast<To>(std::declval<From>()), void())>: std::true_type{};

template <typename Base, typename Derived, typename = void>
struct is_virtual_base_of: std::false_type{};

template <typename Base, typename Derived>
struct is_virtual_base_of<Base, Derived, typename std::enable_if<
    std::is_base_of<Base, Derived>::value && 
    !can_static_cast<Base*, Derived*>::value
>::type>: std::true_type{};

struct Base{};
struct NonVirtual: Base{};
struct Virtual: virtual Base{};

static_assert(is_virtual_base_of<Base, NonVirtual>::value == false);
static_assert(is_virtual_base_of<Base, Virtual>::value == true);

在 Godbolt 上看到它:https://godbolt.org/z/qnT6aq