Template template parameters with variadic templates

前端 未结 1 652
青春惊慌失措
青春惊慌失措 2021-01-06 23:48

For the sake of clarity, I\'ve removed things like the constructor & destructor etc from the below where they don\'t add anything to the question. I have a base class t

相关标签:
1条回答
  • 2021-01-07 00:13

    You've got your ellipsis in the wrong place. Try:

    template<template<typename...> class... Args>
                                        ^^^ here
    

    However, you don't actually want template template parameters; since DerivedSystem1<1> is a type, not a template, you just want ordinary typename parameters:

    template<typename... Args>
    class ContainerClass {
    

    For the actual container, you can't use vector<PeripheralSystem> as that is homogeneous and will slice the derived types down to PeripheralSystem. If you add a virtual destructor to PeripheralSystem you can use vector<unique_ptr<PeripheralSystem>>:

    template<typename... Args>
    class ContainerClass {
      public:
        ContainerClass() : container{std::make_unique<Args>()...} {}
        std::vector<std::unique_ptr<PeripheralSystem>> container;
    };
    

    However, tuple would work just as well and result in fewer allocations:

    template<typename... Args>
    class ContainerClass {
      public:
        ContainerClass() : container{Args{}...} {}
        std::tuple<Args...> container;
    };
    
    0 讨论(0)
提交回复
热议问题