diff options
Diffstat (limited to 'src/rvalue_01.cpp')
-rw-r--r-- | src/rvalue_01.cpp | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/src/rvalue_01.cpp b/src/rvalue_01.cpp new file mode 100644 index 0000000..67a2599 --- /dev/null +++ b/src/rvalue_01.cpp @@ -0,0 +1,161 @@ +/* + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2020 Gothel Software e.K. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include <iostream> +#include <cassert> +#include <iterator> +#include <string> +#include <cctype> +#include <atomic> + +class Bar { + + private: + static std::atomic_int name_counter; + const int name; + + public: + int n; + std::string s; + + Bar() + : name(name_counter.fetch_add(1)), n(0), s() { + std::cout << "default-constructed " << name << std::endl; + } + Bar(const int i, const std::string &str) + : name(name_counter.fetch_add(1)), n(i), s(str) { + std::cout << "custom-constructed " << name << ": " << i << ", " << str << std::endl; + } + + ~Bar() { + std::cout << "destructed " << name << std::endl; + } + + Bar(const Bar &o) noexcept + : name(name_counter.fetch_add(1)), n(o.n), s(o.s) + { + std::cout << "copy-constructed " << name << std::endl; + } + + Bar(Bar &&o) noexcept + : name(name_counter.fetch_add(1)), n(std::move(o.n)), s(std::move(o.s)) + { + std::cout << "move-constructed " << name << std::endl; + } + + Bar& operator=(Bar o) noexcept { + n = o.n; + std::swap(s, o.s); + std::cout << "copy-and-swap-assigned " << o.name << " -> " << name << std::endl; + return *this; + } + + Bar& operator=(const Bar &o) noexcept { + if( &o == this ) { + std::cout << "copy-assigned(same) " << o.name << " -> " << name << std::endl; + return *this; + } else { + n = o.n; + s = o.s; + std::cout << "copy-assigned(copy) " << o.name << " -> " << name << std::endl; + return *this; + } + } + + Bar& operator=(Bar &&o) noexcept { + n = o.n; + s = std::move(o.s); + std::cout << "move-assigned " << o.name << " -> " << name << std::endl; + return *this; + } +}; + +std::atomic_int Bar::name_counter(0); + + +/** Best: Uses RVO or elision */ +Bar foo01() { + Bar localObj; + return localObj; +} + +/** Best: Uses RVO or elision */ +Bar foo02(int c) { + if( !c ) { + return Bar(); + } else { + return Bar(); + } +} + + +/** Uses implicit move-ctor */ +Bar foo11(int c) { + Bar a, b; + if( !c ) { + return a; + } else { + return b; + } +} + +/** Uses explicit move-ctor */ +Bar foo12(int c) { + Bar a, b; + if( !c ) { + return std::move(a); + } else { + return std::move(b); + } +} + +int main() { + int b=1; + std::cout << "Block-" << b << " foo01 Start" << std::endl ; + { + Bar obj = foo01(); + } + std::cout << "Block-" << (b++) << " foo01 End" << std::endl ; + + std::cout << "Block-" << b << " foo02 Start" << std::endl ; + { + Bar obj = foo02(0); + } + std::cout << "Block-" << (b++) << " foo02 End" << std::endl ; + + std::cout << "Block-" << b << " foo11 Start" << std::endl ; + { + Bar obj = foo11(0); + } + std::cout << "Block-" << (b++) << " foo11 End" << std::endl ; + + std::cout << "Block-" << b << " foo12 Start" << std::endl ; + { + Bar obj = foo12(0); + } + std::cout << "Block-" << (b++) << " foo12 End" << std::endl ; + + return 0; + +} |