diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/move/example/doc_template_assign.cpp | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/move/example/doc_template_assign.cpp')
-rw-r--r-- | src/boost/libs/move/example/doc_template_assign.cpp | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/boost/libs/move/example/doc_template_assign.cpp b/src/boost/libs/move/example/doc_template_assign.cpp new file mode 100644 index 00000000..e1959a93 --- /dev/null +++ b/src/boost/libs/move/example/doc_template_assign.cpp @@ -0,0 +1,98 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2014-2014. +// 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) +// +// See http://www.boost.org/libs/move for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#include <boost/move/detail/config_begin.hpp> +#include <boost/move/detail/meta_utils_core.hpp> + +#include <boost/move/move.hpp> + +//[template_assign_example_foo_bar + +class Foo +{ + BOOST_COPYABLE_AND_MOVABLE(Foo) + + public: + int i; + explicit Foo(int val) : i(val) {} + + Foo(BOOST_RV_REF(Foo) obj) : i(obj.i) {} + + Foo& operator=(BOOST_RV_REF(Foo) rhs) + { i = rhs.i; rhs.i = 0; return *this; } + + Foo& operator=(BOOST_COPY_ASSIGN_REF(Foo) rhs) + { i = rhs.i; return *this; } //(1) + + template<class U> //(*) TEMPLATED ASSIGNMENT, potential problem + //<- + #if 1 + typename ::boost::move_detail::disable_if_same<U, Foo, Foo&>::type + operator=(const U& rhs) + #else + //-> + Foo& operator=(const U& rhs) + //<- + #endif + //-> + { i = -rhs.i; return *this; } //(2) +}; +//] + +struct Bar +{ + int i; + explicit Bar(int val) : i(val) {} +}; + + +//<- +#ifdef NDEBUG +#undef NDEBUG +#endif +//-> +#include <cassert> + +int main() +{ +//[template_assign_example_main + Foo foo1(1); + //<- + assert(foo1.i == 1); + //-> + Foo foo2(2); + //<- + assert(foo2.i == 2); + Bar bar(3); + assert(bar.i == 3); + //-> + foo2 = foo1; // Calls (1) in C++11 but (2) in C++98 + //<- + assert(foo2.i == 1); + assert(foo1.i == 1); //Fails in C++98 unless workaround is applied + foo1 = bar; + assert(foo1.i == -3); + foo2 = boost::move(foo1); + assert(foo1.i == 0); + assert(foo2.i == -3); + //-> + const Foo foo5(5); + foo2 = foo5; // Calls (1) in C++11 but (2) in C++98 + //<- + assert(foo2.i == 5); //Fails in C++98 unless workaround is applied + assert(foo5.i == 5); + //-> +//] + return 0; +} + + +#include <boost/move/detail/config_end.hpp> |