2

명확성을 위해 아래의 질문에 아무 것도 추가하지 않는 곳에서 생성자 & 소멸자 등을 제거했습니다. 파생 된 템플릿 클래스에 대한 공통 조상을 만드는 데 사용되는 기본 클래스가 있습니다.가변 템플릿이있는 템플릿 템플릿 매개 변수

class PeripheralSystemBase { 
public: 
    virtual void someFunctionThatsCommonToAllPeripherals() {} 
}; 

template <class T, uint32_t numPeripherals = 1> 
class PeripheralSystem : public PeripheralSystemBase { 
public: 
    PeripheralSystem() : vec(T) {} 
    std::vector<T> vec; // different types of T is the reason why I need to template this class 
}; 

// A & B declaration & definition are irrelevant here 
class A{}; 
class B{}; 

// There are multiple different derived variants of PeripheralSystem 
// At the moment, each has different template parameters 

template <uint32_t customisableParam1> 
class DerivedSystem1 : public PeripheralSystem<A, 1> { 
public: 
    DerivedSystem1() : PeripheralSystem<A, 1>() {} 
}; 

template <uint32_t customisableParam1, uint8_t customisableParam2> 
class DerivedSystem2 : public PeripheralSystem<B, 1> { 
public: 
    DerivedSystem2() : PeripheralSystem<B, 1>() {/*maybe use customisableParam2 here */} 
}; 

은 이제 I 2 개 템플릿 클래스, 동일한 상위 클래스 번 타입 A를 포함하는 벡터, 타입 B의 다른 함유 유래의 각을 갖고; 각각에는 다른 템플릿 매개 변수가 있습니다. 여태까지는 그런대로 잘됐다.

지금 질문드립니다. 아무도 하나 또는 그 안에 PeripheralSystem 파생 된 버전 중 하나 이상을 포함하도록 컨테이너 템플릿을 만들 수 싶습니다 및이 variadic 템플릿을 사용할 수 있습니다 생각하지만 조금 붙어있어 지난 하루 정도의 구문. 컴파일 시간에 컨테이너 클래스의 인스턴스를 만들 수 있기를 바랍니다. 아마 같은 것을 : 내가 먹을수록 내가 사용하고있어 가변 형식이 바로 아니라는 것을 알고

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

// possible usage 
ContainerClass<DerivedSystem1<1>> cc1; 
ContainerClass<DerivedSystem2<2, 3>> cc2; 
ContainerClass<DerivedSystem1<1>, DerivedSystem2<2, 3>> cc3; 

는 :

error: expected ',' or '>' in template-parameter-list template< template class args ...> >

내가 컴파일러에게 노력하고있어 내가 원하는 것입니다 가변 개수의 템플리트 매개 변수를 템플리트에 제공하십시오. 각 템플리트 매개 변수에는 가변 개수의 템플리트 매개 변수가 있습니다. variadic 템플릿으로이 작업을 수행 할 수 있습니까? 올바른 구문에 대한 제안 사항은 무엇입니까?

+0

당신이 C++ (11)를 사용하는 :

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

그러나, tuple 그냥 잘 작동 적은 할당이 발생할 것인가? – Icarus3

+0

예 (컴파일러 스위치가 설정된다는 의미에서). 아니, 나는 그것을 올바르게 사용하는 방법을 알고 있다는 의미에서, 나는 단지 그것에 변화를 만들고있다. – John

답변

2

잘못된 부분에 줄임표가 있습니다. 시도해보십시오.

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

그러나 실제로 템플릿 템플릿 매개 변수는 필요하지 않습니다. DerivedSystem1<1>이 형이 아닌 템플릿이기 때문에, 당신은 단지 일반적인 유형 이름 매개 변수를 원하는 : 그 균일하고 PeripheralSystem 아래로 파생 된 형식을 저밀로

실제 컨테이너
template<typename... Args> 
class ContainerClass { 

, 당신은 vector<PeripheralSystem>을 사용할 수 없습니다. 당신이 PeripheralSystem에 가상 소멸자를 추가 할 경우 당신은 vector<unique_ptr<PeripheralSystem>>를 사용할 수 있습니다

template<typename... Args> 
class ContainerClass { 
    public: 
    ContainerClass() : container{Args{}...} {} 
    std::tuple<Args...> container; 
}; 
+0

C++ 11 튜플의 최대 요소 수에는 제한이 있습니까? Google은 명확하지 않은 것으로 보입니다. 이전 버전은 10 개로 제한됩니다. – John

+0

@ John은 그렇게하지 않습니다. 더 오래된 한계는 variadics가 완전히 지원되지 않았을 때였을 것입니다. '튜플 (tuple) '은 템플리트에 대한 템플리트 매개 변수만큼 많은 요소를 지원해야하며 최소 1024 개가 권장됩니다 (부록 B). – ecatmur

+0

환상적입니다. 많은 도움을 주셔서 대단히 감사합니다. – John

관련 문제