繁体   English   中英

带有可变参数模板的静态继承

[英]Static inheritance with variadic templates

我正在尝试使用std::conditional来实现静态继承。 我的child类有两个可能的父类, parent_one ,它应该根据传递的类型保存多个变量,以及parent_two ,它需要两种类型。 我正在使用标签调度来区分我想要继承的类。

现在到了问题。 当我调用child并将其标记为从parent_one继承两种类型时,它按预期工作。 但是,如果我尝试将任意数量的类型传递给child并打算从parent_one继承, parent_one出现错误:

static_polymorphism.cpp: In instantiation of ‘class child<foo_type, int, int, double, float>’:
static_polymorphism.cpp:110:41:   required from here
static_polymorphism.cpp:99:7: error: wrong number of template arguments (4, should be 2)
   99 | class child : public std::conditional_t<
      |       ^~~~~
static_polymorphism.cpp:90:7: note: provided for ‘template<class T, class F> class parent_two’
   90 | class parent_two {
      |       ^~~~~~~~~~
static_polymorphism.cpp: In function ‘int main(int, char**)’:
static_polymorphism.cpp:111:9: error: ‘class child<foo_type, int, int, double, float>’ has no member named ‘log’
  111 |   first.log();

如果我理解正确,编译器应该根据我的标签调度生成代码。 这意味着它应该创建重载类 - N(基于传递的类型)来自parent_one和 M(基于传递的类型)来自parent_two 但是由于某种原因,我不知道,它不接受类型的变量计数。 你能告诉我我做错了什么吗?

实现就在这里。

using one_t = struct foo_type{};
using two_t = struct bar_type{};

template <typename ... TYPES>
class parent_one {
public:
  parent_one() = default;
  void log() {
    std::cout << "parent_one" << std::endl;
  }
};

template <typename T, typename F>
class parent_two {
public:
  parent_two() = default;
  void log() {
    std::cout << "parent_two" << std::endl;
  }
};

template <typename T, typename ... ARGS>
class child : public std::conditional_t<
    std::is_same_v<T, one_t>,
    parent_one<ARGS...>,
    parent_two<ARGS...>
  >
{
public:
  child() = default;
};

int main(int argc, char *argv[]) {
  child<one_t, int, int, double, float> first;
  first.log();

  child<two_t, int, int> second;
  second.log();
  return 0;
}

std::conditional_t<
    std::is_same_v<T, one_t>,
    parent_one<ARGS...>,
    parent_two<ARGS...>
>

在这里,在检查条件之前验证两个备选方案。 std::conditional_t并不神奇,它只是一个常规模板,它需要所有模板参数都有效才能执行任何操作。

您需要延迟将模板参数替换到父模板中,直到选择了其中一个选项之后。 这是一种可能的解决方案:

template <template <typename...> typename T>
struct delay
{
    template <typename ...P>
    using type = T<P...>;
};
// ...
class child :
    public std::conditional_t<
        std::is_same_v<T, one_t>,
        delay<parent_one>,
        delay<parent_two>
    >::template type<ARGS...> 
{
    // ...
};

你可能很经典:

template<class T, class... Args> struct child: parent_one<Args...> {};

template<class T, class A, class B> struct child<T, A, B>: parent_two<A, B> {};

template<class A, class B> struct child<one_t, A, B>: parent_one<A, B> {};

(这两个特化可以通过requires (!std::is_same_v<T, one_t>)合并为一个)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM