You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This code below can work on gcc, but compile failed on clang++ (version=19.1.7).
#include<ranges>structmy_vector
{
voidemplace_back(auto&&...) { /*assume we do something here*/; }
};
intmain ( )
{
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
The text was updated successfully, but these errors were encountered:
This code below can work on gcc, but compile failed on clang++ (version=19.1.7).
According to
https://en.cppreference.com/w/cpp/ranges/to
,ranges::toable
(non-recursive) iff container implementscontainer(range&&)
, orcontainer(std::from_range_t, range&&)
, orcontainer(iterator, sentinel)
, orcontainer-appendable
.container-appendable
iff container has member functionemplace_back()
orpush_back()
oremplace()
orinsert()
.What libc++ did:
.../c++/v1/__ranges/to.h
__container_insertable
concept, which only accepts a container who implements eitherpush_back()
orinsert()
. libc++ did not careemplace_back()
andemplace()
.ranges::copy(__range, ranges::__container_inserter<range_reference_t<_Range>>(__result));
, which then callsback_insert_iterator
(if push_back) orinsert_iterator
(if insert).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:
Thank you
The text was updated successfully, but these errors were encountered: