c++类型特征:确保子类实现一个方法。

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

有一个虚拟类C。

我想确保任何继承自C的具体子类都能实现一个函数 "get"(如果没有实现,会有一个明显的编译时错误)。

在C中添加一个虚拟的 "get "函数在这种情况下是行不通的,因为C的子类可以实现各种标志的get函数。

(在我正在研究的特殊情况下,pybind11将被用来创建子类的绑定,而pybind11是B的 "get "方法的健壮,可以有多种签名)

在编译时检查一个类是否有函数,可以通过类型特征来完成,如

    template<class T>
    using has_get = 
        decltype(std::declval<T&>().get(std::declval<int>()));

我的问题是,我应该在代码中的什么地方添加一个静态断言(或者其他)来检查 "get "函数的存在。理想情况下,这应该是C声明的一部分,因为对于新的用户代码来说,继承它的事情应该很容易。也可能是完全不同的方法会更好,我想听听。

c++ inheritance typetraits
1个回答
1
投票

不知道你使用的是什么标准,但是在C++20中,你可以使用概念来做这样的事情。

template<typename T>
concept HasGet = requires (T a)
{
  a.get();
};

template<HasGet T>
void foo(T x)
{
  x.get();
}

struct Foo
{
  int get() {
    return 1;
  }
};

struct Bar
{
};

int main()
{
  foo(Foo{});
  foo(Bar{});
}

错误。

<source>: In function 'int main()':

<source>:27:12: error: use of function 'void foo(T) [with T = Bar]' with unsatisfied constraints

   27 |   foo(Bar{});

      |            ^

<source>:8:6: note: declared here

    8 | void foo(T x)

      |      ^~~

<source>:8:6: note: constraints not satisfied

<source>: In instantiation of 'void foo(T) [with T = Bar]':

<source>:27:12:   required from here

<source>:2:9:   required for the satisfaction of 'HasGet<T>' [with T = Bar]

<source>:2:18:   in requirements with 'T a' [with T = Bar]

<source>:4:9: note: the required expression 'a.get()' is invalid

    4 |   a.get();

EDIT:由于C++14是首选,如果我理解你的要求,这是你在C++14中可以做的事情。

#include <type_traits>
#include <utility>

using namespace std;

template<typename... Ts>
using void_t = void;

template<typename T, typename = void>
struct has_get
  : false_type
{};

template<typename T>
struct has_get<T, void_t<decltype(declval<T>().get())>>
  : true_type
{};

template<typename T>
static constexpr auto has_get_v = has_get<T>::value;

struct P
{

};

struct C1 : P
{
  int get()
  {
    return 1;
  }
};

struct C2 : P
{
  float get()
  {
    return 1.0F;
  }
};

struct C3
{
  bool get()
  {
    return true;
  }
};

template<typename T>
enable_if_t<is_base_of<P, decay_t<T>>::value && has_get_v<decay_t<T>>> foo(T x)
{
  x.get();
}

int main()
{
  foo(C1{});
  foo(C2{});
  foo(C3{});
}

错误:

<source>: In function 'int main()':

<source>:61:11: error: no matching function for call to 'foo(C3)'

   61 |   foo(C3{});

      |           ^

<source>:52:77: note: candidate: 'template<class T> std::enable_if_t<(std::is_base_of<P, typename std::decay<_Tp>::type>::value && has_get<typename std::decay<_Tp>::type>::value)> foo(T)'

   52 | enable_if_t<is_base_of<P, decay_t<T>>::value && has_get<decay_t<T>>::value> foo(T x)

      |                                                                             ^~~

<source>:52:77: note:   template argument deduction/substitution failed:

In file included from <source>:1:

/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/type_traits: In substitution of 'template<bool _Cond, class _Tp> using enable_if_t = typename std::enable_if::type [with bool _Cond = false; _Tp = void]':

<source>:52:77:   required by substitution of 'template<class T> std::enable_if_t<(std::is_base_of<P, typename std::decay<_Tp>::type>::value && has_get<typename std::decay<_Tp>::type>::value)> foo(T) [with T = C3]'

<source>:61:11:   required from here

/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/type_traits:2554:11: error: no type named 'type' in 'struct std::enable_if<false, void>'

 2554 |     using enable_if_t = typename enable_if<_Cond, _Tp>::type;
© www.soinside.com 2019 - 2024. All rights reserved.