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

3600. Making istream_iterator copy constructor trivial is an ABI break

Section: 23.6.2.2 [istream.iterator.cons] Status: New Submitter: Jonathan Wakely Opened: 2021-09-23 Last modified: 2021-10-17

Priority: 3

View all other issues in [istream.iterator.cons].

View all issues with New status.

Discussion:

Libstdc++ never implemented this change made between C++03 and C++11 (by N2994):

24.6.1.1 [istream.iterator.cons] p3:
istream_iterator(const istream_iterator<T,charT,traits,Distance>& x) = default;

-3- Effects: Constructs a copy of x. If T is a literal type, then this constructor shall be a trivial copy constructor.

This breaks our ABI, as it changes the argument passing convention for the type, meaning this function segfaults if compiled with today's libstdc++ and called from one that makes the triviality change:

#include <iterator>
#include <istream>

int f(std::istream_iterator<int> i)
{
  return *i++;
}

As a result, it's likely that libstdc++ will never implement the change.

There is no reason to require this constructor to be trivial. It was required for C++0x at one point, so the type could be literal, but that is not true in the current language. We should strike the requirement, to reflect reality. MSVC and libc++ are free to continue to define it as defaulted (and so trivial when appropriate) but we should not require it from libstdc++. The cost of an ABI break is not worth the negligible benefit from making it trivial.

[2021-09-30; Jonathan revises wording after reflector discussion]

Previous resolution [SUPERSEDED]:

This wording is relative to N4892.

  1. Modify 23.6.2.2 [istream.iterator.cons] as indicated:

    istream_iterator(const istream_iterator& x) = default;
    

    -5- Postconditions: in_stream == x.in_stream is true.

    -6- Remarks: If is_trivially_copy_constructible_v<T> is true, then this constructor is trivial.

[2021-10-14; Reflector poll]

Set priority to 3 after reflector poll.

Proposed resolution:

This wording is relative to N4892.

  1. Modify the class synopsis in 23.6.2.1 [istream.iterator.general] as indicated:

    constexpr istream_iterator();
    constexpr istream_iterator(default_sentinel_t);
    istream_iterator(istream_type& s);
    constexpr istream_iterator(const istream_iterator& x) = default;
    ~istream_iterator() = default;
    istream_iterator& operator=(const istream_iterator&) = default;
    
  2. Modify 23.6.2.2 [istream.iterator.cons] as indicated:

      constexpr istream_iterator(const istream_iterator& x) = default;
    

    -5- Postconditions: in_stream == x.in_stream is true.

    -6- Remarks: If is_trivially_copy_constructible_v<T> is true, then this constructor is trivial. If the initializer T(x.value) in the declaration auto val = T(x.value); is a constant initializer ([expr.const]), then this constructor is a constexpr constructor.