Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a standard library implementation specialize standard types?

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<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>

- 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).

like image 307
bolov Avatar asked Jul 03 '15 15:07

bolov


People also ask

What is the purpose of C++ Standard Library?

The C++ Standard Library provides several generic containers, functions to use and manipulate these containers, function objects, generic strings and streams (including interactive and file I/O), support for some language features, and functions for everyday tasks such as finding the square root of a number.

What is included in namespace std?

In C++, a namespace is a collection of related names or identifiers (functions, class, variables) which helps to separate these identifiers from similar identifiers in other namespaces or the global namespace. The identifiers of the C++ standard library are defined in a namespace called std .

Why is using namespace std used in C++?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.


1 Answers

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:

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

like image 177
Tommy Andersen Avatar answered Oct 28 '22 16:10

Tommy Andersen