Namespaces
Variants

std::valarray<T>::operator+,-,~,!

cppreference.net에서
 
 
 
 
valarray<T> operator+() const;
(1)
valarray<T> operator-() const;
(2)
valarray<T> operator~() const;
(3)
valarray<bool> operator!() const;
(4)

수치 배열의 각 요소에 단항 연산자를 적용합니다.

매개변수

(없음)

반환값

* this 에 있는 값들에 해당 연산자를 적용하여 얻은 값들을 요소로 가지는 숫자 배열.

예외

구현 정의 예외를 던질 수 있습니다.

참고

각 연산자는 다음 요구 사항이 충족되는 경우에만 인스턴스화할 수 있습니다:

  • 지정된 연산자는 T 타입에 적용될 수 있어야 합니다.
  • 결과 값이 T(1-3) 또는 bool(4)로 명확하게 변환될 수 있어야 합니다.

반환 타입이 std::valarray와 다른 함수로 구현될 수 있습니다. 이 경우, 대체 타입은 다음 속성을 가집니다:

  • 모든 const 멤버 함수는 std::valarray에 대해 제공됩니다.
  • std::valarray, std::slice_array, std::gslice_array, std::mask_array 및 std::indirect_array는 대체 타입에서 생성될 수 있습니다.
  • const std::valarray<T>& 를 매개변수로 사용하는 모든 함수에 대해 (단, begin()과 end()(C++11부터)는 제외) 동일한 함수가 대체 타입을 사용하여 추가되어야 합니다.
  • 두 개의 const std::valarray<T>& 인수를 사용하는 모든 함수에 대해, const std::valarray<T>&과 대체 타입의 모든 조합을 사용하는 동일한 함수가 추가되어야 합니다.
  • 반환 타입은 가장 깊게 중첩된 인수 타입보다 두 수준 이상의 템플릿 중첩을 추가하지 않아야 합니다.

예제

#include <iostream>
#include <string_view>
#include <valarray>
template<typename T>
void print(std::string_view const note,
           std::valarray<T> const vala, // by-value, see Notes above
           std::string_view const term = "\n")
{
    std::cout << note << std::boolalpha << std::showpos;
    for (T const element : vala)
        std::cout << '\t' << element;
    std::cout << term;
}
int main()
{
    std::valarray<int> x{1, 2, 3, 4};
    print<int>("x: ", x);
    print<int>("+x: ", +x);
    print<int>("+ + x: ", + + x);
    print<int>("-x: ", -x);
    print<int>("- - x: ", - - x, "\n\n");
    std::valarray<short> y{0, 1, -1, 0x7fff};
    print<short>("y: ", y);
    print<short>("~y: ", ~y);
    print<short>("~~y: ", ~~y, "\n\n");
    std::valarray<bool> z{true, false};
    print<bool>("z: ", z);
    print<bool>("!z: ", !z);
    print<bool>("!!z: ", !!z);
}

가능한 출력:

x:      +1      +2      +3      +4
+x:     +1      +2      +3      +4
+ + x:  +1      +2      +3      +4
-x:     -1      -2      -3      -4
- - x:  +1      +2      +3      +4
y:      +0      +1      -1      +32767
~y:     -1      -2      +0      -32768
~~y:    +0      +1      -1      +32767
z:      true    false
!z:     false   true
!!z:    true    false

참고 항목

복합 할당 연산자를 valarray의 각 요소에 적용
(public member function)
두 valarray의 각 요소에, 또는 valarray와 값에 이항 연산자를 적용
(function template)