标准库实现可以专门化标准类型吗?

时间:2021-08-20 17:07:07

For instance, let's assume for the sake of the argument that a more efficient (storage, operations on it) implementation for a vector of integral types is found (compared to the generic vector implementation). Can a standard complying library do something like:

例如,让我们假设为一个整数类型的向量找到了一个更有效的实现(存储,对它进行操作)(与通用的向量实现相比)。一个标准的相符库可以做以下事情:

template <class T, class A, class Enable = void>
class vector { ... };

template <class T>
class vector<T, A, std::enable_if_t<std::is_integral<T>::value>> { ... };

I think that this would be illegal because of the extra template parameter.

我认为这是非法的,因为有额外的模板参数。

But what about a little compiler magic: (aside from the extra implementation work) would something like that be allowed:

但是对于一个小的编译器魔法呢?(除了额外的实现工作)是否允许这样的东西:

  • vector<integral_type, A> to be internally mapped to class vector_integral<T, A> while
  • 向量 被内部映射到类vector_integral ,一个> ,一个>
  • vector<anything_else, A> to be internally mapped to class vector<T, A>.
  • 向量 内部映射到类向量 ,一个> ,一个>

- This is obviously not about specializations explicitly mentioned in the standard, like std::vector<bool>

-这显然不是标准中明确提到的专门化,比如std::vector

- The specialization would obviously have the same interface and observable behavior.

-专门化显然具有相同的接口和可观察的行为。

- Let's ignore concepts, as they are not yet standard. Unless you have some facts.

-让我们忽略概念,因为它们还不是标准的。除非你有一些事实。

- This is a pure academic question (read personal curiosity).

-这是一个纯粹的学术问题。

1 个解决方案

#1


1  

As long as the standard library implementation lives up to the guarantees issued by the standard, it is allowed to have specializations. These guarantees obviously include growth complexity, interface functions, and for certain classes the internal layout of the data.

只要标准库实现符合标准发布的保证,就允许有专门化。这些保证显然包括增长复杂性、接口功能以及数据的内部布局。

The vector<bool> specialization is, to my knowledge, included in the standard since its internal data layout differs from the generic vector<T> library class, thus it needs to be specifically allowed by the standard.

根据我的知识,向量 专门化是在标准中包含的,因为它的内部数据布局与泛型向量 库类不同,因此它需要被标准明确地允许。

The standtard explicitly says 17.5.1.4 ad. 7:

该标准明确规定了17.5.1.4 ad。7:

Complexity requirements specified in the library clauses are upper bounds, and implementations that provide better complexity guarantees satisfy the requirements.

在库子句中指定的复杂性需求是上界,提供更好的复杂性保证的实现满足需求。

#1


1  

As long as the standard library implementation lives up to the guarantees issued by the standard, it is allowed to have specializations. These guarantees obviously include growth complexity, interface functions, and for certain classes the internal layout of the data.

只要标准库实现符合标准发布的保证,就允许有专门化。这些保证显然包括增长复杂性、接口功能以及数据的内部布局。

The vector<bool> specialization is, to my knowledge, included in the standard since its internal data layout differs from the generic vector<T> library class, thus it needs to be specifically allowed by the standard.

根据我的知识,向量 专门化是在标准中包含的,因为它的内部数据布局与泛型向量 库类不同,因此它需要被标准明确地允许。

The standtard explicitly says 17.5.1.4 ad. 7:

该标准明确规定了17.5.1.4 ad。7:

Complexity requirements specified in the library clauses are upper bounds, and implementations that provide better complexity guarantees satisfy the requirements.

在库子句中指定的复杂性需求是上界,提供更好的复杂性保证的实现满足需求。