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
|
//
// shared_pointer_reinterpret_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>
struct X
{};
int main()
{
{
boost::shared_ptr<char> pc;
boost::shared_ptr<int> pi = boost::reinterpret_pointer_cast<int>(pc);
BOOST_TEST(pi.get() == 0);
boost::shared_ptr<X> px = boost::reinterpret_pointer_cast<X>(pc);
BOOST_TEST(px.get() == 0);
}
{
boost::shared_ptr<int> pi(new int);
boost::shared_ptr<char> pc = boost::reinterpret_pointer_cast<char>(pi);
boost::shared_ptr<int> pi2 = boost::reinterpret_pointer_cast<int>(pc);
BOOST_TEST(pi.get() == pi2.get());
BOOST_TEST(!(pi < pi2 || pi2 < pi));
BOOST_TEST(pi.use_count() == 3);
BOOST_TEST(pc.use_count() == 3);
BOOST_TEST(pi2.use_count() == 3);
}
{
boost::shared_ptr<X> px(new X);
boost::shared_ptr<char> pc = boost::reinterpret_pointer_cast<char>(px);
boost::shared_ptr<X> px2 = boost::reinterpret_pointer_cast<X>(pc);
BOOST_TEST(px.get() == px2.get());
BOOST_TEST(!(px < px2 || px2 < px));
BOOST_TEST(px.use_count() == 3);
BOOST_TEST(pc.use_count() == 3);
BOOST_TEST(px2.use_count() == 3);
}
return boost::report_errors();
}
|