Namespaces
Variants

std::mask_array<T>::operator=

来自 zh.cppreference.net

 
 
 
 
 
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)

为所有引用的元素赋值。

1) 将 value 赋值给所有元素。
2) 将 val_arr 的元素赋值给 *this。
3) 将从 other_arr 选择的元素赋值给 *this。

参数

value - 要赋值给所有被引用元素的值
val_arr - 要赋值的 std::valarray
other_arr - 要赋值的 std::mask_array

返回值

1,2) (无)
3) * this

示例

#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);
}

输出:

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

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用版本 发布时行为 正确行为
LWG 123 C++98 重载 (2) 为非 const 改为 const
LWG 253 C++98 拷贝赋值运算符为 private 改为 public
LWG 621 C++98 拷贝赋值运算符为非 const 改为 const