简体   繁体   English

为什么 STL 容器适配器需要值和容器类型作为模板参数?

[英]Why do STL container adapters require both value and container type as template parameters?

不能从Container::value_type推导出第一个参数T吗?

That would mean that to be used in a container adaptor, a container must provide a nested typedef value_type .这意味着要在容器适配器中使用,容器必须提供嵌套的 typedef value_type Certainly all of the containers in the standard library do so - but that's not the universe of possible containers.当然,标准库中的所有容器都这样做 - 但这不是可能的容器的范围。

Right now, stack requires: Any sequence container supporting operations back() , push_back() and pop_back() can be used to instantiate stack .现在, stack需要: 任何支持back()push_back()pop_back()操作的序列容器都可以用于实例化stack

Container adapters are older thst C++ itself.容器适配器比 C++ 本身更旧。

And they are a neater idea than they are practical.它们是一个比实际更简洁的想法。 So they tend to be pretty neglected in my experience.因此,根据我的经验,它们往往被忽视。

Plus, some unfortunate ABI choices of major vendors make adding or changing default parameters to templates an ABI breaking issue.此外,主要供应商的一些不幸的 ABI 选择使添加或更改模板的默认参数成为 ABI 破坏性问题。

So, we get pre 20th century template arguments and no value_type deduction.所以,我们得到了 20 世纪之前的模板参数,没有 value_type 推论。

Also, stack<T> seems like cleaner syntax than stack<deque<T>> anyhow.此外,无论如何, stack<T>似乎比stack<deque<T>>简洁的语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM