STL - does every compiler implement it differently?

前端 未结 5 1678
臣服心动
臣服心动 2020-12-18 13:48

It was said to me that the standard template library is differently implemented by every compiler, is this correct?

How can computational complexity (both in time an

5条回答
  •  旧巷少年郎
    2020-12-18 14:07

    First things first, you probably mean the C++ standard library, not STL. The STL was a library written before C++ was standardised that heavily influenced the C++ standard library.

    Now, the C++ standard provides rules and definitions that an implementation should conform to. In particular, the standard library is described as various partial class definitions and function declarations and the properties that they should have. The implementation is free to implement the library in any way it chooses, as long as it meets exactly what the standard says. Here's what the standard says about a conforming implementation (§1.4):

    For classes and class templates, the library Clauses specify partial definitions. Private members (Clause 11) are not specified, but each implementation shall supply them to complete the definitions according to the description in the library Clauses.

    For functions, function templates, objects, and values, the library Clauses specify declarations. Implementations shall supply definitions consistent with the descriptions in the library Clauses.

    For example, to ensure the complexity of a std::list implementation is equal to that of a double-linked list, its member functions are given complexity requirements. For example, the std::list::insert functions are given the following requirement (§23.3.5.4, emphasis added):

    Insertion of a single element into a list takes constant time and exactly one call to a constructor of T.

    This doesn't necessarily mean that std::list must be implemented as a double-linked list. However, this is a common choice. An implementation must only act in such a way that the requirements are met (or that it appears that the requirements have been met; the as-if rule).

提交回复
热议问题