为什么std :: stack不使用模板模板参数?

Hed*_*ede 36 c++ templates

为什么std::stackstd::queue使用类型模板参数,而不是模板的模板参数为他们的基础容器类型?

即为什么stack声明如下:

template<typename T, typename Container = deque<T>>
class stack;
Run Code Online (Sandbox Code Playgroud)

但不是这样的:

template<typename T, template<typename> class Container = deque>
class stack;
Run Code Online (Sandbox Code Playgroud)

rub*_*nvb 38

因为通常容器std::vector具有多个模板参数.通过不关心它是一个模板,你允许使用每种容器.

怎么会

template<class T, class Allocator = std::allocator<T>> class vector;
Run Code Online (Sandbox Code Playgroud)

适合

template<typename> class Container
Run Code Online (Sandbox Code Playgroud)

就像你在你的stack?(提示:它没有!)你需要特殊情况来处理你想要支持的每个数字和类型的模板参数(类型与非类型),这很愚蠢,因为这些通常不会贡献任何比简单更多的信息

typename Container
Run Code Online (Sandbox Code Playgroud)

请注意,以获得在如实际模板参数std::vector,你有类型定义std::vector::value_typestd::vector::allocator_type,除去具有提供这些类型明确,你实际使用的类型的需要(即Containerstack).

  • @Hedede还要考虑甚至不是从模板中实例化的容器.使用你的设计你不能做像`std :: stack <double,MySpecialContainerWhichOnlyWorksForDoubles>`这样的东西. (16认同)
  • 它比"每个_number_模板aguments的特殊情况"要糟糕得多,因为模板模板参数可能混合了类型和非类型模板参数.即你不仅可以拥有`Container <Type1,Type2>`而且还可以拥有`Container <Type1,Int2>`.这使得情况更加糟糕. (2认同)

Hol*_*olt 18

简而言之:因为使用模板模板参数比使用类型参数更具限制性*而不提供任何优势.

*限制性我的意思是你可能需要一个更复杂的东西来获得与"简单"类型参数相同的结果.

为什么没有优势?

std::stack可能有这样的属性:

template <typename T, typename Container>
struct stack {
    Container container;
};
Run Code Online (Sandbox Code Playgroud)

如果您Container使用模板模板参数替换,为什么会获得?

template <typename T, template <typename...> class Container>
struct stack {
    Container<T> container;
};
Run Code Online (Sandbox Code Playgroud)

您只实例化Container一次且仅针对T(Container<T>),因此模板模板参数没有优势.

为什么它更具限制性?

使用模板模板参数,您必须传递给std::stack公开相同签名的模板,例如:

template <typename T, template <typename> class Container>
struct stack;

stack<int, std::vector> // Error: std::vector takes two template arguments
Run Code Online (Sandbox Code Playgroud)

也许你可以使用可变参数模板:

template <typename T, template <typename... > class Container>
struct stack {
    Container<T> container;
};

stack<int, std::vector> // Ok, will use std::vector<int, std::allocator<int>>
Run Code Online (Sandbox Code Playgroud)

但是如果我不想使用标准std::allocator<int>怎么办?

template <typename T, 
          template <typename....> class Container = std::vector, 
          typename Allocator = std::allocator<T>>
struct stack {
    Container<T, Allocator> container;
};

stack<int, std::vector, MyAllocator> // Ok...
Run Code Online (Sandbox Code Playgroud)

这变得有点乱......如果我想使用我自己的容器模板需要3/4/N参数怎么办?

template <typename T,
          template <typename... > class Container = std::vector,
          typename... Args>
struct stack {
    Container<T, Args...> container;
};

stack<int, MyTemplate, MyParam1, MyParam2> // Ok...
Run Code Online (Sandbox Code Playgroud)

但是,如果我想使用非模板化容器怎么办?

struct foo { };
struct foo_container{ };

stack<foo, foo_container> // Error!

template <typename... >
using foo_container_template = foo_container;

stack<foo, foo_container_template> // Ok...
Run Code Online (Sandbox Code Playgroud)

使用类型参数没有这样的问题1:

stack<int>
stack<int, std::vector<int, MyAllocator<int>>
stack<int, MyTemplate<int, MyParam1, MyParam2>>
stack<foo, foo_container>
Run Code Online (Sandbox Code Playgroud)

1还有其他情况不适用于模板模板参数,例如使用模板接受特定顺序中的类型和非类型参数的混合,您可以为其创建通用模板模板参数,甚至使用可变参数模板.


nat*_*ate 18

使用模板模板参数会将可用作底层容器的类型限制为公开相同模板签名的类型.只要这些表单支持预期的接口,它就允许任意类型.


Kon*_*lph 13

因为它不编译:

std::deque 不是类型

template <typename T> class std::deque
Run Code Online (Sandbox Code Playgroud)

它的类型

template<class T, class Alloc> class std::deque
Run Code Online (Sandbox Code Playgroud)

这当然是一个更普遍的问题:即使我们要将Alloc模板参数提供给我们的stack类模板,该类现在只能使用具有两个类型模板参数的容器.这是一个不合理的限制.