dimanche 18 novembre 2018

Did not get a compile time error after deleting copy/move constructors and assignment operators during passing object to function

I the following code I have explictly forbidden copying and moving Dummy object using delete specifier for copy and move constructors and for copy and move assignment operators:

#include <string>
#include <iostream>

struct Dummy
{
    explicit Dummy(const std::string& value) : value_(value) {}
    Dummy(const Dummy&) = delete;
    Dummy& operator=(const Dummy&) = delete;
    Dummy(Dummy&&) = delete;
    Dummy& operator=(Dummy&&) = delete;

    void print_this_address() const
    {
        std::cout << "this address: " << this << "\n";
    }

    std::string value_;
};

void foo(Dummy&& obj)
{
    std::cout << obj.value_ << "\n";
    obj.print_this_address();
}

int main()
{
    Dummy obj("42");
    obj.print_this_address();
    foo(std::move(obj));
    std::cout << obj.value_ << "\n";
    return 0;
}

This code compiles and runs well and I'm getting the following output in my terminal:

this address: 0x7ffeeb6d2a20
42
this address: 0x7ffeeb6d2a20
42

Can someone explains for me what's happening in this code and why I didn't get compile error and have the same object in foo function as in main. function?

Aucun commentaire:

Enregistrer un commentaire