C++ Logo

std-proposals

Advanced search

[std-proposals] better container::append_range?

From: Hewill Kang <hewillk_at_[hidden]>
Date: Sat, 15 Nov 2025 14:33:08 +0800
Hi all,

In C++23, we can use `append_range` to append ranges to a container more
intuitively and simply. Its function signature is as follows (from
https://eel.is/c++draft/containers)

```
template<container-compatible-range<T> R>
  constexpr void append_range(R&& rg);
```

So we can:

```
std::vector<int> v;
v.append_range(std::views::iota(0, 5));
```

I think there are two areas for improvement.
First, it would be great to provide a default template to support
`initializer_list`:

```
template<container-compatible-range<T> R = initializer_list<T>>
  constexpr void append_range(R&& rg);
```

which would make it more convenient for users:

```
std::vector<int> v;
v.append_range({1, 2, 3, 4});
```

Additionally, it might be more valuable if it could return `*this` instead
of `void`:

```
template<container-compatible-range<T> R = initializer_list<T>>
  constexpr vector& append_range(R&& rg);
```

which can provide a chain operation for users, since
`string::append_range` already returns `string&`:

```
std::vector<int> v;
v.append_range({1, 2, 3}).append_range({4, 5, 6})..append_range({7, 8, 9});
```

Do these two enhancements make sense? Any thoughts?

Thanks,
Hewill

Received on 2025-11-15 06:33:26