【问题标题】:C++ Type erasure with traits具有特征的 C++ 类型擦除
【发布时间】:2017-12-18 10:42:13
【问题描述】:

我想知道这是否可以创建一个符合这样一个特征的擦除类型:

template<class T>
using read_t = std::conditional_t<true,
    decltype(std::declval<T>().Read(uint16_t{})),
    std::integral_constant<uint8_t (T::*)(uint16_t), &T::Read>>;

并像这样使用它?

using any_readable_t = any<read_t>; 
auto test(any_readable_t &r) -> uint8_t {
     return r.Read(0);
}

【问题讨论】:

标签: c++ c++17 type-erasure stdany


【解决方案1】:

没有任何外部库,需要做很多工作才能实现您想要的。使用 Louis Dionne 的dyno

DYNO_INTERFACE(Readable,
    (read, uint8_t (uint16_t))
);

auto test(Readable& r) -> uint8_t {
    return r.read(0);
}

【讨论】:

  • 是的,我知道这个库,但由于缺少 HanaConfig.cmake,我无法使其与 CMake 一起使用:/
  • @uknys:在 GitHub 上打开一个问题? github.com/ldionne/dyno/issues
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 2020-11-01
  • 1970-01-01
相关资源
最近更新 更多