Skip to content
C++: Remove list entries while retaining a surviving reference

C++: Remove list entries while retaining a surviving reference

November 11, 2026

A linked list can remove selected nodes while references to surviving nodes remain valid. Its member remove_if also reports the number removed in C++20. This can help when another part of a program already refers to a record that must stay.

Prepare a scratch directory

Tested on Linux with GCC 14.2.0 in C++20 mode. You need Bash, a C++20 compiler and standard library, a text editor, and the mktemp, rm, and rmdir utilities. Keep the same terminal open. Stop if setup fails, before saving the source:

start_directory="$PWD"
work_directory="$(mktemp -d)" || exit 1
cd -- "$work_directory" || exit 1

Run the example

Save as cpp-list-remove-if-retained-reference.cpp:

#include <cassert>
#include <iostream>
#include <list>
#include <string>

struct Task { std::string name; bool done; };

int main() {
    std::list<Task> tasks{{"keep", false}, {"old-a", true},
                          {"later", false}, {"old-b", true}};
    Task& retained = tasks.front();
    const auto removed = tasks.remove_if([](const Task& task) {
        return task.done;
    });
    assert(removed == 2 && tasks.size() == 2);
    assert(&retained == &tasks.front());
    retained.name = "active";
    std::cout << "removed: " << removed << '\n';
    for (const auto& task : tasks) std::cout << task.name << '\n';
    assert(tasks.remove_if([](const Task& task) { return task.done; }) == 0);
    std::list<Task> empty;
    assert(empty.remove_if([](const Task&) { return true; }) == 0);
}

Compile and run:

g++ -std=c++20 -Wall -Wextra -Werror -pedantic cpp-list-remove-if-retained-reference.cpp -o cpp-list-remove-if-retained-reference &&
./cpp-list-remove-if-retained-reference

Observed output:

removed: 2
active
later

How it works

The saved reference points at the first node, whose predicate result is false. Removing other nodes does not invalidate that reference. The later assignment changes the surviving task and appears in the printed list. References and iterators to removed nodes would be invalid.

Use the list member function for this operation. The generic std::remove_if algorithm moves retained values through a range and does not itself erase container nodes; that is a different identity model. The count returned by the member function requires C++20.

Limits and choices

References remain valid only while the referenced node survives and the owning list remains alive. Concurrent mutation still requires synchronization. Keep this predicate free of structural changes to the list. A list has allocation and locality costs compared with a vector; choose it when the ownership and reference-stability requirements justify those costs.

The C++ working draft specifies the library operation used here. The example uses the C++20 interface tested above; newer draft additions are outside its scope.

Clean up

rm -- cpp-list-remove-if-retained-reference.cpp cpp-list-remove-if-retained-reference
cd -- "$start_directory" || exit 1
rmdir -- "$work_directory"
unset start_directory work_directory

If the directory is not empty, inspect the remaining files before removing anything else.

Acknowledgements

This post was created with assistance from AI.

Last updated on