std::mask_array::operator=(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸŽ€ std::mask_array::operator=(3) πŸŽ€

okay bestie, welcome to the C++ Standard Libary page for std::mask_array::operator=(3). This is the assignment operator for a mask_array, and there are three overloads. Let’s get into it ✨


πŸŽ€ NAME πŸŽ€

std::mask_array::operator= β€” literally std::mask_array::operator=. That’s the name, bestie, no cap πŸ’…


🌸 Synopsis 🌸

There are three overloads, and yes, all of them are const:

void operator=( const T& value ) const;                           (1)
void operator=( const std::valarray<T>& val_arr ) const;          (2)
const mask_array& operator=( const mask_array& other_arr ) const; (3)

The whole vibe here: assigns values to all referred elements. Overload by overload:

  1. Assigns value to all of the elements.
  2. Assigns the elements of val_arr to the referred to elements of *this.
  3. Assigns the selected elements from other_arr to the referred to elements of *this.

πŸ’– Parameters πŸ’–

  • value β€” a value to assign to all of the referred elements.
  • val_arr β€” std::valarray to assign.
  • other_arr β€” std::mask_array to assign.

πŸ” Return value πŸ”

  • 1,2) β€” (none). No return, bestie.
  • 3) β€” *this. Yup, it returns itself.

πŸ§ͺ Example πŸ§ͺ

Run this code, bestie:

// Run this code

#include <iomanip>
#include <iostream>
#include <valarray>

void print(std::valarray<int> const& v)
{
    for (int e : v)
        std::cout << std::setw(2) << e << ' ';
    std::cout << '\n';
}

int main()
{
    const auto init = {1, 2, 3, 4, 5, 6, 7, 8};
    std::valarray<int> v;

    v = init;
    v[(v % 2) == 0] = 0; // (1)
    print(v);

    v = init;
    v[(v % 2) == 1] = std::valarray<int>{-1, -2, -3, -4}; // (2)
    print(v);

    v = init;
    v[(v % 2) == 0] = v[(v % 2) == 1]; // (3)
    print(v);
}

πŸ–₯️ Output πŸ–₯️

 1  0  3  0  5  0  7  0
-1  2 -2  4 -3  6 -4  8
 1  1  3  3  5  5  7  7

🩹 Defect reports 🩹

okay so these behavior-changing defect reports were applied retroactively to previously published C++ standards β€” yes, retroactively, bestie:

  • DR LWG 123 β€” applied to C++98; behavior as published: overload (2) was non-const; correct behavior: made const.
  • DR LWG 253 β€” applied to C++98; behavior as published: the copy assignment operator was private; correct behavior: made public.
  • DR LWG 621 β€” applied to C++98; behavior as published: the copy assignment operator was non-const; correct behavior: made const.

mwah πŸ’‹ β€” footer sign-off time: source http://cppreference.com, version/date 2024.06.10, page std::mask_array::operator=(3). bye bestie, go assign something cute 🫢✨

πŸ“¦ where this page comes from