Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -------------------------------------------------
- // CMakeLists.txt
- // -------------------------------------------------
- cmake_minimum_required(VERSION 3.5)
- project(cpp4 LANGUAGES CXX)
- set(CXX_VER "11")
- set(NO_COPY_ELISION 0)
- set(CMAKE_CXX_FLAGS "-std=c++${CXX_VER} -O0 -DCXX_VER=${CXX_VER}")
- if (NO_COPY_ELISION)
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-elide-constructors")
- endif()
- set(CMAKE_CXX_STANDARD_REQUIRED ON)
- add_executable(cpp4 main.cpp)
- // -------------------------------------------------
- // main.cpp
- // -------------------------------------------------
- #include <iostream>
- #include <utility>
- #include <string>
- #define ENABLE_COPY_CTOR
- //#define ENABLE_MOVE_CTOR
- struct A {
- A() {
- std::cout << "A()" << std::endl;
- }
- A(std::string s): s(s) {
- std::cout << "A(std::string s): " << s << std::endl;
- }
- #ifdef ENABLE_COPY_CTOR
- A(const A& other) {
- std::cout << "A(const A& other): " << s << std::endl;
- this->s = other.s;
- }
- #else
- #if CXX_VER > 03
- A(const A& other) = delete;
- #endif
- #endif
- #if defined(ENABLE_MOVE_CTOR) && CXX_VER > 03
- A(A&& other) {
- std::cout << "A(A&& other)" << std::endl;
- this->s = other.s;
- other.s = "";
- }
- #else
- #if CXX_VER > 03
- A(A&& other) = delete;
- #endif
- #endif
- std::string s;
- };
- A f(bool c = false) {
- //return c ? A("hello") : A("hello2");
- return A("hello");
- }
- int main() {
- A a = f();
- std::cout << a.s << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement