std::is_base_of 和虚拟基类

问题描述 投票:0回答:4

有没有办法判断一个基类是否是虚基类?

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

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

c++ c++11 enable-if
4个回答
1
投票
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;
}

实例


1
投票

使用 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


0
投票

没有看到C++20解决方案,所以有这个极其优雅的概念:

/// Check if a type is virtually derived from all the provided BASE(s)  
template<class T, class...BASE>
concept VirtuallyDerivedFrom = ((::std::is_base_of_v<BASE, T>
        and not requires (BASE* from) { static_cast<T*>(from); }
    ) and ...);

这是证据


0
投票

此处发布的 static_cast 解决方案不适用于

protected/private
继承。

struct B {};
struct D : protected B {};
struct VD : virtual B {};

B * pb = nullptr;
static_cast<D*>(pb);  // error: 'B' is an inaccessible base of 'D'
static_cast<VB*>(pb); // error: cannot convert from pointer to base class 'B' to pointer to derived class 'VB' because the base is virtual

因此,

protected/private
基类将在这些实现中被标识为虚拟基类。

通过检查 is_virtual_base_of 的 Boost 源代码,这是一个有效的实现:

template<typename B, typename D>
struct __vbase_helper : D, virtual B {};

template<typename B, typename D>
struct is_virtual_base_of : std::bool_constant<
    std::is_base_of_v<B, D> &&
    !std::is_same_v<B, D> &&
    sizeof(__vbase_helper<B, D>) == sizeof(D)>
{};
                                                                                                            
template<typename B, typename D>
constexpr bool is_virtual_base_of_v = is_virtual_base_of<B, D>::value;

但是如果你将其用于

protected/private
基类,则会出现警告,这是无害的,但一点也不优雅。

© www.soinside.com 2019 - 2024. All rights reserved.