File: shared_ptr_rv_pointer_cast_test.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (106 lines) | stat: -rw-r--r-- 2,780 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
//  shared_ptr_rv_pointer_cast_test.cpp
//
//  Copyright (c) 2016 Chris Glover
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//

#include <boost/shared_ptr.hpp>
#include <boost/core/lightweight_test.hpp>

#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )

struct X
{};

struct Y: public X
{};

struct U
{
    virtual ~U() {}
};

struct V: public U
{};

struct W : public U
{};

int main()
{
    {
        boost::shared_ptr<X> px(new Y);

        boost::shared_ptr<Y> py1 = boost::static_pointer_cast<Y>(px);
        boost::shared_ptr<Y> py2 = boost::static_pointer_cast<Y>(std::move(px));
        BOOST_TEST(!px);
        BOOST_TEST(px.use_count() == 0);
        BOOST_TEST(py1.get() == py2.get());
        BOOST_TEST(!(py1 < py2 || py2 < py1));
        BOOST_TEST(py1.use_count() == 2);
        BOOST_TEST(py2.use_count() == 2);
    }

    {
        boost::shared_ptr<int const volatile> px(new int);

        boost::shared_ptr<int> px2 = boost::const_pointer_cast<int>(px);
        boost::shared_ptr<int> px3 = boost::const_pointer_cast<int>(std::move(px));
        BOOST_TEST(!px);
        BOOST_TEST(px.use_count() == 0);
        BOOST_TEST(px2.get() == px3.get());
        BOOST_TEST(!(px2 < px3 || px2 < px3));
        BOOST_TEST(px2.use_count() == 2);
        BOOST_TEST(px3.use_count() == 2);
    }

    {
        boost::shared_ptr<char> pv(reinterpret_cast<char*>(new Y));

        boost::shared_ptr<Y> py1 = boost::reinterpret_pointer_cast<Y>(pv);
        boost::shared_ptr<Y> py2 = boost::reinterpret_pointer_cast<Y>(std::move(pv));
        BOOST_TEST(!pv);
        BOOST_TEST(pv.use_count() == 0);
        BOOST_TEST(py1.get() == py2.get());
        BOOST_TEST(!(py1 < py2 || py2 < py1));
        BOOST_TEST(py1.use_count() == 2);
        BOOST_TEST(py2.use_count() == 2);
    }

#if !defined( BOOST_NO_RTTI )
    {
        boost::shared_ptr<U> pu(new V);

        boost::shared_ptr<V> pv1 = boost::dynamic_pointer_cast<V>(pu);
        boost::shared_ptr<V> pv2 = boost::dynamic_pointer_cast<V>(std::move(pu));
        BOOST_TEST(!pu);
        BOOST_TEST(pu.use_count() == 0);
        BOOST_TEST(pv1.get() == pv2.get());
        BOOST_TEST(!(pv1 < pv2 || pv2 < pv1));
        BOOST_TEST(pv1.use_count() == 2);
        BOOST_TEST(pv2.use_count() == 2);
    }

    {
        boost::shared_ptr<U> pu(new V);
        boost::shared_ptr<W> pw = boost::dynamic_pointer_cast<W>(std::move(pu));
        BOOST_TEST(!pw);
        BOOST_TEST(pu);
    }
#endif // !defined( BOOST_NO_RTTI )

    return boost::report_errors();
}

#else // !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )

int main()
{
    return 0;
}

#endif // !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )