Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pavelkryukov/forward_list2

C++ std::forward_list with push_back and before_end
https://github.com/pavelkryukov/forward_list2

boost cpp23 cpp26 forward-list linked-list linked-list-in-cpp linked-lists stl stl-containers

Last synced: about 20 hours ago
JSON representation

C++ std::forward_list with push_back and before_end

Awesome Lists containing this project

README

        

[![codecov](https://codecov.io/gh/pavelkryukov/forward_list2/branch/main/graph/badge.svg?token=5KtoC3WASR)](https://codecov.io/gh/pavelkryukov/forward_list2)

# forward_list2

Wrapper around C++ [`std::forward_list`](https://en.cppreference.com/w/cpp/container/forward_list) which adds an iterator to the last element.

The idea has been proposed and discussed here: [cpp-ru/ideas#487](https://github.com/cpp-ru/ideas/issues/487).

### Extra functions
```c++
const_reference back() const;

const_iterator before_end() const noexcept;
const_iterator cbefore_end() const noexcept;

void push_back(const T& value);
void push_back(T&& value);

template
reference emplace_back(Args&&... args);
```

### Price
* Compute time overheads to maintain the iterator to the last element.
* Extra O(N) traversals on copy assignment and sorting.
* Memory size overhead on empty container:
```c++
static_assert(sizeof(std::forward_list) == sizeof(void*));
static_assert(sizeof(forward_list2) == 2 * sizeof(void*));
```

### Limitations

* No `reference back();` is available since it would require passing non-constant iterator to `erase_after`.

### Impact

* Identified and fixed [PR libstdc++/103853](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103853).