C ++模板模板(双模板?)

| 我想构建一个“ 0”类,以便用户能够选择要用于实现“ 0”的容器。例如,
List/Vector
。 部分代码: 栈
#ifndef STACK_H_
#define STACK_H_

template <typename T, template<typename T> class ContainerType>
class Stack{
    ContainerType<T> container;
public:
    Stack() : container(ContainerType<T>()){}

};

#endif /* STACK_H_ */
测试文件
#include \"stack.h\"
#include <vector>

int main(){   
    Stack<int, std::vector<int> > stack;
    return 0;
}
好吧,它无法编译。我在网上收到下一个错误:
Stack<int, std::vector<int> > stack;
错误:
expected a class template, got `std::vector<int, std::allocator<int> >\' test.cpp

invalid type in declaration before \';\' token test.cpp

type/value mismatch at argument 2 in template parameter 
list for `template<class T, template<class T> class ContainerType> 
class Stack\' test.cpp

‪
    
已邀请:
        首先,它将是
std::vector
,而没有别的,因为
vector
驻留在ѭ9and命名空间中,并且您要输入模板template参数,而
std::vector<int>
不再是模板。接下来,“ 7”实际上需要两个模板参数,一个用于类型,另一个用于分配器:
template <
    typename T,
    template<typename, typename> class ContainerType,
    typename Alloc = std::allocator<T>
>
class Stack{
  ContainerType<T, Alloc> container;
  // ...
};

// usage:
Stack<int, std::vector> s;
现在,这仅启用具有两个模板参数作为基础类型的容器,因此您最好使用标准的功能:将其作为普通类型:
template <typename T, typename ContainerType>
class Stack{
  ContainerType container;
  // ...
};

// usage:
Stack<int, std::vector<int> > s;
为确保基础类型具有相同的“ 14”,您可以执行伪造的“静态声明”,或者如果您具有启用了C ++ 0x的编译器,则可以执行实际的静态声明:
#include <tr1/type_traits> // C++03 us std::tr1::is_same
//#include <type_traits> // C++0x, use std::is_same

template <typename T, typename ContainerType>
class Stack{
  typedef typename ContainerType::value_type underlying_value_type;
  typedef char ERROR_different_value_type[
               std::tr1::is_same<T, underlying_value_type>::value ? 1 : -1
                                         ]
  ContainerType container;
  // ...
};
之所以起作用,是因为如果
T
与使用的容器的
T
不同,它将是
typedef char ERROR_different_vale_type[-1]
,并且可能不存在负大小的数组,这会导致编译器错误。 :)现在,使用C ++ 0x,您只需
static_assert
即可:
#include <tr1/type_traits> // C++03
//#include <type_traits> // C++0x

template <typename T, typename ContainerType>
class Stack{
  typedef typename ContainerType::value_type underlying_value_type;
  static_assert(std::tr1::is_same<T, underlying_value_type>::value,
    \"Error: The type of the stack must be the same as the type of the container\");
  ContainerType container;
  // ...
};
为了方便起见,您现在可以为常见情况指定默认的模板参数:
template <typename T, typename ContainerType = std::vector<T>>
class Stack{
  ContainerType container;
  // ...
};

// usage:
Stack<int> s;
现在,您可以只使用
std::stack
来做到这一点(尽管它使用
std::deque
作为基础类型)。 :)     
        由于容器的稀疏性问题,最简单的方法是不使用模板template参数。 相反,只需传递完整的容器类型,就可以了。然后提取
value_type
(标准STL内部typedef)以获取该值。
template <typename Container>
class Stack
{
public:
  typedef typename Container::value_type value_type;

private:
  Container _container;
}; // class Stack<Container>
然后,您可以简单地将其用作
Stack< std::vector<int> >
,其中将包含
int
。     
        由于ѭ8属于
std
命名空间,因此必须对其进行限定。但是除此之外,由于
ContainerType
是模板模板参数,您需要传递模板而不是最终类型:
Stack<int, std::vector > stack;
    
        这行:
     Stack<int, vector<int> > stack;
应该:
  Stack<int, std::vector<int> > stack;
或者您可以在
test.cpp
之前加上前缀
 using namespace std;
    

要回复问题请先登录注册