Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is everything in C++11 STL user-implementable?

Tags:

c++

c++11

I know that libs like <map>, <vector> and <algorithm> can be implemented by user even if they do not exist in the standard libraries.

But is some of the classes or functions in C++11 STL just "compiler-magic" that cannot be implemented manually?


EDIT By STL, I meant the standard template libraries, which do not requires extra linking.

like image 235
RnMss Avatar asked Apr 11 '15 12:04

RnMss


People also ask

Does C++ 11 support STL?

array - C++11 now has an STL array. It stores n elements of type T contiguously in memory. It differs from a vector as it cannot be resized once created.

Is it good to use STL?

STL is well tested and reliable but it is not the fastest solution. Some of the containers allocate a lot of small memory blocks rather than one big block. If you really have a speed problem then you may consider making your own fixed-size list. But for many purposes STL is the standard solution.

Can STL be used in C?

C can't have an "exact equivalent" of STL because C doesn't have templates or classes.

What does C++ STL include?

The Standard Template Library (STL) is a software library originally designed by Alexander Stepanov for the C++ programming language that influenced many parts of the C++ Standard Library. It provides four components called algorithms, containers, functions, and iterators.


1 Answers

If you mean the part of the standard library that originated from Alexander Stepanov's Standard Template Library, then you can implement all of it without any compiler "magic" (i.e., you only need normal functionality that's required by the rest of the C++ standard).

Algorithms operate on iterators. The operations they need from the iterator are defined by the class of iterator on which they operate (see below). The algorithm simply carries out normal operations such as assigning to elements and swapping elements.

All iterators provide a few operations such as increment and dereference. Bidirectional iterators also provide decrement, and random access iterators provide addition/subtraction. None of these is particularly complex it implement using standard compiler features.

Containers are similar to algorithms in that they mostly operate on data via iterators. They also use allocator classes to allocate memory, create objects in that memory, and so on. Although writing an allocator used to be fairly complex (and the requirements poorly documented) C++11 has simplified the task considerably (and it never required anything non-standard).

If you take "STL" to mean the STandard Library in general, then yes, there are quite a few parts that require some compiler magic of various sorts.

Some of these are defined in terms of lower-level components, so (for example) iostreams are defined in terms of C's getchar/puthchar1 to get data from/to the outside world. Other parts (e.g., type traits, std::uncaught_exception) provide an interface to data that's generated by the compiler, but isn't available to portable code by other means.


1. Note, however, that even though iostreams are defined in terms of reading and writing via C functions, they're not required to actually use those C functions, just to do the same things as if they had.

like image 174
Jerry Coffin Avatar answered Oct 08 '22 20:10

Jerry Coffin