简体   繁体   中英

Is there an equivalent of c++ shared_ptr in swift 3?

I know strong (default) and weak (with the weak keyword) references in swift 3, but is there an equivalent of shared references?

Thanks

The memory management paradigm in Swift is different from C++ since it inherits the retain-release mechanism (through ARC) from Objective-C. As you might expect, C++'s solution puts less responsibility on the compiler, is more expressive and optimised but also more complex to work with.

So, to answer your question: strong (which is default) works basically like shared_ptr , weak is like weak_ptr and unique_ptr doesn't have a direct equivalent. However, some strong vars might act like unique_ptr if the compiler is able to guarantee the uniqueness of the pointer (eg you create and destroy an object in the same scope - like a function's body - without assigning it to any var)

Of course, this only applies to reference types. Value types are just copied.

Swift applies ARC (automatic reference counting) of strong references to decide when to free up memory used by a reference type instance (namely, when the number of strong references to that object is zero). ARC and its reference counting runs automatically, but holds similiarities to the explicit use of C++'s std::shared_ptr ; the latter will allow an object pointed to (by shared ptrs) to be destroyed (eg by a supplied deletor) only when all smart pointers pointing to the object has gone out of scope or been explicitly reset (/nulled).

In the example above, you can consider the strong immutables (references) foo and bar (the latter return from foobar() ) as the std::smart_ptr 's equivalents of C++: they both point to the same Foo object, and only when both are out of scope will the object be deinitialized.

class Foo {
    init() { print("initializing") }
    deinit { print("deinitialized") }
}

func foobar() -> Foo {
    let foo = Foo() // strong reference
    let bar = foo   // another strong reference 
    // total "shared" count is now 2
    return bar
    // reference associateced with 'foo' goes 
    // out of scope, but the reference associateced
    // with 'bar' is returned from the function, and
    // thus, ARC still keeps the Foo instance alive.
}

func bar() {
    print("calling foobar ...")
    let bar = foobar() // retains the reference from 'bar' in foobar()
    print("foo() is now out of scope")
    print("leaving bar() scope ...")
} // 'bar' of bar() goes out of scope: Foo object should be deinitialized

bar()
/* calling foobar ...
   initializing
   foo() is now out of scope
   leaving bar() scope ...
   deinitialized             */
/* ^^^^^^^^^^^^^- ok */

普通变量 (没有弱变量或无变量变量 )具有与shared_ptr类似的语义。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM