This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++11 status.

1278. Inconsistent return values for forward_list::insert_after

Section: [forward.list.modifiers] Status: C++11 Submitter: Bo Persson Opened: 2009-11-25 Last modified: 2023-02-07

Priority: Not Prioritized

View all other issues in [forward.list.modifiers].

View all issues with C++11 status.


After applying LDR149, forward_list now has 5 overloads of insert_after, all returning an iterator.

However, two of those - inserting a single object - return "An iterator pointing to a copy of x [the inserted object]" while the other three - inserting zero or more objects - return an iterator equivalent to the position parameter, pointing before any possibly inserted objects.

Is this the intended change?

I don't really know what insert_after(position, empty_range) should really return, but always returning position seems less than useful.

[ 2010-02-04 Howard adds: ]

I agree this inconsistency will be error prone and needs to be fixed. Additionally emplace_after's return value is unspecified.

[ 2010-02-04 Nico provides wording. ]

[ 2010 Pittsburgh: ]

We prefer to return an iterator to the last inserted element. Modify the proposed wording and then set to Ready.

[ 2010-03-15 Howard adds: ]

Wording updated and set to Ready.

Proposed resolution:

In forward_list modifiers [forwardlist.modifiers] make the following modifications:

iterator insert_after(const_iterator position, size_type n, const T& x);


10 Returns: position. An iterator pointing to the last inserted copy of x or position if n == 0.

template <class InputIterator>
  iterator insert_after(const_iterator position, InputIterator first, InputIterator last);


13 Returns: position. An iterator pointing to the last inserted element or position if first == last.

iterator insert_after(const_iterator position, initializer_list<T> il);


15 Returns: position. An iterator pointing to the last inserted element or position if il is empty.

template <class... Args>
  iterator emplace_after(const_iterator position, Args&&... args);


17 ...

Returns: An iterator pointing to the new constructed element from args.