Skip to content

libc++: std::ranges::to does not match standards. #127307

Closed as duplicate of#105322
Closed as duplicate of#105322
@AnonymousPC

Description

@AnonymousPC

This code below can work on gcc, but compile failed on clang++ (version=19.1.7).

#include <ranges>

struct my_vector
{
    void emplace_back(auto&&...) { /*assume we do something here*/; }
};

int main ( )
{
    auto my_vec = std::vector<int>() | std::ranges::to<my_vector>();
}
  • According to https://en.cppreference.com/w/cpp/ranges/to,

    • A container is ranges::toable (non-recursive) iff container implements
      • constructor container(range&&), or
      • constructor container(std::from_range_t, range&&), or
      • constructor container(iterator, sentinel), or
      • satisfies concept container-appendable.
    • We now focus on the 4th case.
    • In standard, A container is container-appendable iff container has member function emplace_back() or push_back() or emplace() or insert().
    • Then, we should use a for-loop to put the items into this container one-by-one.
  • What libc++ did:

    • in file .../c++/v1/__ranges/to.h
      • define __container_insertable concept, which only accepts a container who implements either push_back() or insert(). libc++ did not care emplace_back() and emplace().
      • put items into container through ranges::copy(__range, ranges::__container_inserter<range_reference_t<_Range>>(__result));, which then calls back_insert_iterator(if push_back) or insert_iterator(if insert).
      • back_insert_iterator has operator=(container::value_type&&) to do this push_back(), but what if the container(which might be trivial) who does not typedef value_type? same do insert_iterator.
  • What is expected:

    • Accepts all 4 kinds of "container-appendable" container.
    • get rid of the fancy insert_iterator, an use a trivial for-loop to append elements one by one. (it only takes <= 5 lines)

Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateResolved as duplicatelibc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.rangesIssues related to `<ranges>`

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions