具有附加可选模板参数的标准库容器?

2021-12-13 00:00:00 standards parameters templates c++ stl

多次阅读文章中的声明 - 我想将此问题添加到 Stackoverflow,并询问社区 - 以下代码是否可移植?

Having read the claim multiple times in articles - I want to add this question to Stackoverflow, and ask the community - is the following code portable?

template<template<typename T, typename Alloc> class C>
void f() {
  /* some code goes here ... */
}

int main() {
  f<std::vector>();
}

提供 std::vector 的实现是否真的允许有除了两个众所周知的模板参数之外的额外的、默认的模板参数?这会使上面的代码格式错误,因为它假定有两个模板参数.有关此类声明的示例,请参阅本文的最后一段.

Is the implementation that supplies std::vector really allowed to have additional, defaulted template parameters beyond the two well known ones? This would render the above code ill-formed, as it assumes two template parameters. See the last paragraph in this article for an example of such a claim.

推荐答案

我发现了以下 问题报告,上面写着

I found the following issue report, which says

没有歧义;标准是明确的.不允许库实现者向标准库类添加模板参数.这不属于好像"规则,因此只有在标准为实施者提供明确许可的情况下才允许这样做.这需要改变标准.

There is no ambiguity; the standard is clear as written. Library implementors are not permitted to add template parameters to standard library classes. This does not fall under the "as if" rule, so it would be permitted only if the standard gave explicit license for implementors to do this. This would require a change in the standard.

LWG 决定不进行此更改,因为它会破坏涉及模板模板参数或标准库类模板专业化的用户代码.

The LWG decided against making this change, because it would break user code involving template template parameters or specializations of standard library class templates.

说一个实现可以添加其他可选参数的书籍和人似乎是错误的.

The books and people that say an implementation may add other optional parameters seem to be wrong.

相关文章