Shared pointer to an immutable type has value semantics

前端 未结 4 1097
Happy的楠姐
Happy的楠姐 2020-12-12 13:47

Sean Parent gave a talk at Going Native 2013 titled Inheritance Is The Base Class of Evil. At 20 minutes, 50 seconds in, he makes the statement that a shared pointer to an i

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-12 14:28

    I am giving 3 examples. In all three cases I create a variable a with the content "original value". Then I create another variable b by saying auto b = a; and after this statement I assign a the content "new value".

    If a and b have value semantics, I expect b's content to be the "original content". And in fact exactly this happens with string and shared_ptr. The conceptual meaning of auto b = a; is the same with these types. Not so much with shared_ptr, b will have the content "new value".

    Code (online demo):

    #include 
    #include 
    #include 
    
    using namespace std;
    
    void string_example() {
    
        auto a = string("original value");
    
        auto b = a; // true copy by copying the value
    
        a = string("new value");
    
        cout << "a = " << a << endl;
        cout << "b = " << b << endl;
        cout << boolalpha << "&a == &b ? " << (&a==&b) << endl;
    }
    
    void shared_ptr_example() {
    
        auto a = make_shared("original value");
    
        auto b = a; // not a copy, just and alias
    
        *a = string("new value"); // and this gonna hurt b
    
        cout << "a = " << *a << endl;
        cout << "b = " << *b << endl;
        cout << boolalpha << "&a == &b ? " << (&a==&b) << endl;
    }
    
    void shared_ptr_to_const_example() {
    
        auto a = make_shared("original value");
    
        auto b = a;
    
        //*a = string("new value"); // <-- now won't compile
        a = make_shared("new value");
    
        cout << "a = " << *a << endl;
        cout << "b = " << *b << endl;
        cout << boolalpha << "&a == &b ? " << (&a==&b) << endl;
    }
    
    int main() {
    
        cout << "--------------" << endl;
        cout << "string example" << endl;
        string_example();
    
        cout << "------------------" << endl;
        cout << "shared_ptr example" << endl;
        shared_ptr_example();
    
        cout << "---------------------------" << endl;
        cout << "shared_ptr to const example" << endl;
        shared_ptr_to_const_example();
    }
    

    Output:

    --------------
    string example
    a = new value
    b = original value
    &a == &b ? false
    ------------------
    shared_ptr example
    a = new value
    b = new value
    &a == &b ? false
    ---------------------------
    shared_ptr to const example
    a = new value
    b = original value
    &a == &b ? false
    

    Having said that, I wish he had had a little more time: There are a few things I am still wondering about after that presentation. I am pretty convinced it was only the lack of time, he seems like a excellent presenter.

提交回复
热议问题