std::common...r::operator=(3)
π std::common...r::operator=(3) π
okay bestie, welcome to std::common...r::operator=(3) from the C++ Standard Libary manual β yes, βLibaryβ is exactly how itβs spelled on the page, donβt come for me. This is the assignment operator page, and itβs lowkey all about copying the underlying variant from another common_iterator and living your truth. π
π NAME π
std::common_iterator::operator=βstd::common_iterator::operator=
(okay the NAME line literally says the same thing twice, and honestly? iconic. we keep it.)
β¨ Synopsis β¨
Hereβs the synopsis, copied exactly because compilers are not gonna vibe with your creative rewrites:
template< class I2, class S2 >
requires std::convertible_to<const I2&, I> &&
std::convertible_to<const S2&, S> &&
std::assignable_from<I&, const I2&> &&
(since C++20)
std::assignable_from<S&, const S2&>
constexpr common_iterator& operator=( const common_iterator<I2, S2>&
x );
Assigns the underlying std::variant member object var from that of x.
Let i be x.var.index(). Then this assignment is equivalent to:
std::get<i>(var) = std::get<i>(x.var), ifvar.index() == i,var.emplace<i>(std::get<i>(x.var))otherwise.
π¨ undefined behavior alert π¨ β the behavior is undefined ifxis in an invalid state, that is,x.var.valueless_by_exception()is equal totrue. bestie do not do this. π
π Parameters π
xβ iterator adaptor to assign from
πΈ Return value πΈ
*this β thatβs it, thatβs the return. π«Ά
π¦ Example π¦
Run this code (the page literally says // Run this code, so weβre keeping the energy):
#include <algorithm>
#include <initializer_list>
#include <iostream>
#include <iterator>
int main()
{
const auto il = {1, 2, 3, 4, 5, 6};
using CI = std::common_iterator<
std::counted_iterator<std::initializer_list<int>::iterator>,
std::default_sentinel_t>;
CI first{std::counted_iterator{std::next(begin(il), 1), ssize(il) -
1}};
const CI first2{std::counted_iterator{std::next(begin(il), 2),
ssize(il) - 2}};
const CI last{std::default_sentinel};
std::copy(first, last, std::ostream_iterator<int>{std::cout, " "});
std::cout << '\n';
first = first2;
std::copy(first, last, std::ostream_iterator<int>{std::cout, " "});
std::cout << '\n';
}
π Output π
2 3 4 5 6
3 4 5 6
π©Ή Defect reports π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- LWG 3574 β applied to: C++20. behavior as published:
variantwas fullyconstexpr(P2231R1) butcommon_iteratorwas not. corrected behavior:common_iteratorwas also madeconstexpr. retroactive glow-up, we love to see it. π₯
π See also π
- constructor β constructs a new iterator adaptor (C++20) (public member function)
mwah π β source: http://cppreference.com, version/date: 2024.06.10, page: std::common...r::operator=(3). bye bestie, go assign responsibly π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms