次の方法で共有


<memory> 演算子

operator!=

オブジェクト間の不等性をテストします。

template <class Type, class Other>
bool operator!=(
    const allocator<Type>& left,
    const allocator<Other>& right) throw();

template <class T, class Del1, class U, class Del2>
bool operator!=(
    const unique_ptr<T, Del1>& left,
    const unique_ptr<U&, Del2>& right);

template <class Ty1, class Ty2>
bool operator!=(
    const shared_ptr<Ty1>& left,
    const shared_ptr<Ty2>& right);

パラメーター

left
不等性をテストする一方のオブジェクト。

right
不等性をテストする一方のオブジェクト。

Ty1
左辺の共有ポインターによって制御される型。

Ty2
右辺の共有ポインターによって制御される型。

戻り値

オブジェクトが等しくない場合は true、オブジェクトが等しい場合は false

解説

1 つ目のテンプレートの演算子は、false を返します。 (すべての既定のアロケーターは等価です。)

2 番目と 3 番目のテンプレート演算子は !(left == right) を返します。

最初のコード例は、アロケーター オブジェクトを比較した結果を示しています。

// memory_op_me.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;

int main( )
{
   allocator<double> Alloc;
   vector <char>:: allocator_type v1Alloc;

   if ( Alloc != v1Alloc )
      cout << "The allocator objects Alloc & v1Alloc not are equal."
           << endl;
   else
      cout << "The allocator objects Alloc & v1Alloc are equal."
           << endl;
}
The allocator objects Alloc & v1Alloc are equal.

次のコード例では、割り当てられたオブジェクトへの共有ポインターを比較します。

// std__memory__operator_ne.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>

int main()
    {
    std::shared_ptr<int> sp0(new int(0));
    std::shared_ptr<int> sp1(new int(0));

    std::cout << "sp0 != sp0 == " << std::boolalpha
        << (sp0 != sp0) << std::endl;
    std::cout << "sp0 != sp1 == " << std::boolalpha
        << (sp0 != sp1) << std::endl;

    return (0);
    }
sp0 != sp0 == false
sp0 != sp1 == true

operator==

オブジェクト同士が等しいかどうかをテストします。

template <class Type, class Other>
bool operator==(
    const allocator<Type>& left,
    const allocator<Other>& right) throw();

template <class Ty1, class Del1, class Ty2, class Del2>
bool operator==(
    const unique_ptr<Ty1, Del1>& left,
    const unique_ptr<Ty2, Del2>& right);

template <class Ty1, class Ty2>
bool operator==(
    const shared_ptr<Ty1>& left;,
    const shared_ptr<Ty2>& right);

パラメーター

left
等しいかどうかをテストするオブジェクト。

right
等しいかどうかをテストするオブジェクト。

Ty1
左辺の共有ポインターによって制御される型。

Ty2
右辺の共有ポインターによって制御される型。

戻り値

オブジェクトが等しい場合は true、オブジェクトが等しくない場合は false

解説

1 つ目のテンプレート演算子は true を返します。 (すべての既定のアロケーターは等価です。)

2 番目と 3 番目のテンプレート演算子は left.get() == right.get() を返します。

最初のコード例は、さまざまな方法で構築されたアロケーター オブジェクトを比較した結果を示しています。

// memory_op_eq.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;

int main( )
{
   allocator<char> Alloc;
   vector <int>:: allocator_type v1Alloc;

   allocator<char> cAlloc(Alloc);
   allocator<int> cv1Alloc(v1Alloc);

   if ( cv1Alloc == v1Alloc )
      cout << "The allocator objects cv1Alloc & v1Alloc are equal."
           << endl;
   else
      cout << "The allocator objects cv1Alloc & v1Alloc are not equal."
           << endl;

   if ( cAlloc == Alloc )
      cout << "The allocator objects cAlloc & Alloc are equal."
           << endl;
   else
      cout << "The allocator objects cAlloc & Alloc are not equal."
           << endl;
}
The allocator objects cv1Alloc & v1Alloc are equal.
The allocator objects cAlloc & Alloc are equal.

次のコード例では、割り当てられたオブジェクトへの共有ポインターを比較します。

// std__memory__operator_eq.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>

int main()
    {
    std::shared_ptr<int> sp0(new int(0));
    std::shared_ptr<int> sp1(new int(0));

    std::cout << "sp0 == sp0 == " << std::boolalpha
        << (sp0 == sp0) << std::endl;
    std::cout << "sp0 == sp1 == " << std::boolalpha
        << (sp0 == sp1) << std::endl;

    return (0);
    }
sp0 == sp0 == true
sp0 == sp1 == false

operator>=

1 つ目のオブジェクトが 2 つ目のオブジェクト以上であるかをテストします。

template <class T, class Del1, class U, class Del2>
bool operator>=(
    const unique_ptr<T, Del1>& left,
    const unique_ptr<U, Del2>& right);

template <class Ty1, class Ty2>
bool operator>=(
    const shared_ptr<Ty1>& left,
    const shared_ptr<Ty2>& right);

パラメーター

left
比較するオブジェクトの 1 つ。

right
比較するオブジェクトの 1 つ。

Ty1
左辺の共有ポインターによって制御される型。

Ty2
右辺の共有ポインターによって制御される型。

解説

このテンプレート演算子は left.get() >= right.get() を返します。

operator<

1 番目のオブジェクトが 2 番目のオブジェクトより小さいかどうかをテストします。

template <class T, class Del1, class U, class Del2>
bool operator<(
    const unique_ptr<T, Del1>& left,
    const unique_ptr<U&, Del2>& right);

template <class Ty1, class Ty2>
bool operator<(
    const shared_ptr<Ty1>& left,
    const shared_ptr<Ty2>& right);

パラメーター

left
比較するオブジェクトの 1 つ。

right
比較するオブジェクトの 1 つ。

Ty1
左辺のポインターによって制御される型。

Ty2
右辺のポインターによって制御される型。

operator<=

1 番目のオブジェクトが 2 番目のオブジェクト以下であるかどうかをテストします。

template <class T, class Del1, class U, class Del2>
bool operator<=(
    const unique_ptr<T, Del1>& left,
    const unique_ptr<U&, Del2>& right);

template <class Ty1, class Ty2>
bool operator<=(
    const shared_ptr<Ty1>& left,
    const shared_ptr<Ty2>& right);

パラメーター

left
比較するオブジェクトの 1 つ。

right
比較するオブジェクトの 1 つ。

Ty1
左辺の共有ポインターによって制御される型。

Ty2
右辺の共有ポインターによって制御される型。

解説

このテンプレート演算子は left.get() <= right.get() を返します。

operator>

1 番目のオブジェクトが 2 番目のオブジェクトより大きいかどうかをテストします。

template <class Ty1, class Del1, class Ty2, class Del2>
bool operator>(
    const unique_ptr<Ty1, Del1>& left,
    const unique_ptr<Ty2&, Del2gt;& right);

template <class Ty1, class Ty2>
bool operator>(
    const shared_ptr<Ty1>& left,
    const shared_ptr<Ty2>& right);

パラメーター

left
比較するオブジェクトの 1 つ。

right
比較するオブジェクトの 1 つ。

Ty1
左辺の共有ポインターによって制御される型。

Ty2
右辺の共有ポインターによって制御される型。

operator<<

共有ポインターをストリームに書き込みます。

template <class Elem, class Tr, class Ty>
std::basic_ostream<Elem, Tr>& operator<<(std::basic_ostream<Elem, Tr>& out,
    shared_ptr<Ty>& sp);

パラメーター

Elem
ストリーム要素の型。

Tr
ストリーム要素の特性の型。

Ty
共有ポインターによって制御される型。

out
出力ストリーム。

sp
共有ポインター。

解説

このテンプレート関数は out << sp.get() を返します。

// std__memory__operator_sl.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>

int main()
    {
    std::shared_ptr<int> sp0(new int(5));

    std::cout << "sp0 == " << sp0 << " (varies)" << std::endl;

    return (0);
    }
sp0 == 3f3040 (varies)