How do I know if I am using a copy or moving?

Do I understand correctly that the standard C ++ 14 library uses move semantics? In other words, can I be sure that instead of copying in the following program I use the transition:

#include <iostream>
#include <string>
#include <vector>

using namespace std::string_literals;

std::vector<std::string> greeting()
{
    std::vector<std::string> vs {"hello"s, "world"s};
    return vs;
}

int main()
{
    std::vector<std::string> s = greeting();
    std::cout << s[0] << " " << s[1] << "\n" ;
}

Is there any way to check?

As in the following example:

#include <iostream>
#include <string>
#include <vector>

using namespace std::string_literals;

class Greeting {
    public:
    std::string first, second;
    Greeting() { first = "hello"s ; second = "world"s ;};
};

Greeting greetingc()
{
    Greeting g;
    return g;
}

int main()
{
    Greeting g = greetingc();
    std::cout << g.first << " " << g.second << "\n" ;
}

Move or copy?

+4
source share
1 answer

. , , . , . , - (, , ), ( , , ...).

, RVO, . move, , (rvalue semantics), std:: move. - , . .

, , .

Greeting(const Greeting& g)
{
    std::cout << "Copy";
    // Copy it
}

Greeting(Greeting&& g)
{
    std::cout << "Move";
    // Move it
}

, .

Greeting foo(){ Greeing a; return a; }
Greeting a; // Normal construction
Greeting b(a); // Copy of 'a'
Greeting c(std::move(a)); // Move, 'a' will be changed
Greeting d(foo()); // Move from rvalue which is returned from foo() unless RVO kicks in
+2

All Articles