模板类使用参数包时如何传递其他模板参数? [英] How to pass other template parameter when template class uses parameter pack?

查看:108
本文介绍了模板类使用参数包时如何传递其他模板参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个模板类,为作为模板参数传递的每种类型实现 print()方法。

I would like to create template class that implements print() method for each type passed as template parameters.

类似的东西:

class Interface
{
public:
    virtual ~Interface() = default;
    virtual void print(int) = 0;
    virtual void print(double) = 0;
};
X x<int, double, Interface>;

X类具有公共方法 void print()即可。

下面的整个代码:

#include <iostream>
#include <type_traits>

struct Printer
{
    void print(int i) {std::cout << i << std::endl; }
    void print(double d) {std::cout << d << std::endl; } 
};

class Interface
{
public:
    virtual ~Interface() = default;
    virtual void print(int) = 0;
    virtual void print(double) = 0;
};

template <typename... Args>
class X;

template <typename Interface>
class X<Interface> : public Interface
{
    static_assert(std::is_abstract<Interface>::value, "Last argument should be an interface");

public:
    X(Printer printer) {}
    using Interface::print;
};

template <typename Arg, typename... Args>
class X<Arg, Args...> : public X<Args...>
{
    using Parent = X<Args...>;

public:
    using Parent::print;

    X(Printer printer_): Parent(printer), printer{printer_} {}
    void print(Arg arg) override { printer.print(arg); }

private:
    Printer printer;
};

int main()
{
    Printer printer;
    X<double, int, Interface> x(printer);
    x.print(5);
}

您看到的是类X 使用 Printer 类,但是问题是我想将 Printer 作为模板参数...

As you see class X uses Printer class but the problem is that I would like to have Printer as a template parameter...

有可能吗?

推荐答案


如您所见,类X使用Printer类,但问题是我想将Printer作为模板参数...

As you see class X uses Printer class but the problem is that I would like to have Printer as a template parameter...

可以吗?

对不起,但是...我看不到问题(通过Story Teller的简化建议:放置一个 Printer 对象)。

Sorry but... I don't see the problem (with a great simplification suggested by Story Teller: place a single Printer object in the ground case case)

template <typename...>
class X;

template <typename Printer, typename Interface>
class X<Printer, Interface> : public Interface
 {
   static_assert(std::is_abstract<Interface>::value,
                 "Last argument should be an interface");

   public:
      X (Printer p0) : printer{p0}
       { }

      using Interface::print;  // why?

   protected:
      Printer printer;
 };

template <typename Printer, typename Arg, typename... Args>
class X<Printer, Arg, Args...> : public X<Printer, Args...>
 {
   using Parent = X<Printer, Args...>;

   public:
      using Parent::print;
      using Parent::printer;

      X(Printer printer_): Parent{printer_} {}

      void print(Arg arg) override { printer.print(arg); }
 };

// ....

X<Printer, double, int, Interface> x(printer);

关闭主题:注意:您正在使用打印机未初始化

Off topic: attention: you're using printer uninitialized

X(Printer printer_): Parent(printer), printer{printer_} {}

我想你应该写 Parent(printer _)

这篇关于模板类使用参数包时如何传递其他模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆