명확성을 위해 아래의 질문에 아무 것도 추가하지 않는 곳에서 생성자 & 소멸자 등을 제거했습니다. 파생 된 템플릿 클래스에 대한 공통 조상을 만드는 데 사용되는 기본 클래스가 있습니다.가변 템플릿이있는 템플릿 템플릿 매개 변수
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 템플릿으로이 작업을 수행 할 수 있습니까? 올바른 구문에 대한 제안 사항은 무엇입니까?
당신이 C++ (11)를 사용하는 :
그러나,
tuple
그냥 잘 작동 적은 할당이 발생할 것인가? – Icarus3예 (컴파일러 스위치가 설정된다는 의미에서). 아니, 나는 그것을 올바르게 사용하는 방법을 알고 있다는 의미에서, 나는 단지 그것에 변화를 만들고있다. – John