Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AnonymousPC opened this issue Feb 15, 2025 · 1 comment
Closed

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

AnonymousPC opened this issue Feb 15, 2025 · 1 comment
Labels
duplicate Resolved as duplicate libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. ranges Issues related to `<ranges>`

Comments

@AnonymousPC
Copy link

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

@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Feb 15, 2025
@zwuis
Copy link
Contributor

zwuis commented Feb 15, 2025

This is LWG4016, and was implemented in #113103. Please wait for libc++ 20 release.

@philnik777 philnik777 closed this as not planned Won't fix, can't repro, duplicate, stale Feb 15, 2025
@EugeneZelenko EugeneZelenko added the ranges Issues related to `<ranges>` label Feb 15, 2025
@frederick-vs-ja frederick-vs-ja added the duplicate Resolved as duplicate label Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Resolved as duplicate libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. ranges Issues related to `<ranges>`
Projects
None yet
Development

No branches or pull requests

6 participants