c++ - 模板类使用参数包时如何传递其他模板参数?

标签 c++ templates c++14 variadic-templates template-meta-programming

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

类似的东西:

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

X 类 具有公共(public)方法 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);
}

如您所见,class 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...

Is it possible? How to do that?

抱歉,但是...我没有看到问题(Story Teller 建议进行了极大的简化:在地面案例中放置一个 Printer 对象)

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);

题外话:注意:你正在使用未初始化的printer

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

我想你应该写 Parent(printer_)

关于c++ - 模板类使用参数包时如何传递其他模板参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58712888/

相关文章:

c++ - 编译时枚举字符串整数对

c++ - 区别: std::make_unique<char>(size_t size) 和 std::make_unique<char[]>(size_t size)?

c++ - 返回类型而不是对象如何有效,误解了代码片段

c++ - 没有匹配函数调用 'AnimationSet::AnimationSet()'

c++ - 如何在编译时切换/选择类型?

c++ - 错误的函数名称重整

c++ - 模板类内部类的模板特化

c++ - 嵌套类中的 "Invalid covariant return type"错误,其方法返回基于模板的对象

c++ - Boost.Asio/OpenSSL HTTPS GET 证书问题

c++ - GetComputerName() 未在 Windows 控制台中正确显示 Unicode