简体   繁体   English

在swift 3中是否有相当于c ++的shared_ptr?

[英]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? 我知道swift 3中的强(默认)和弱(弱关键字)引用,但有相同的共享引用吗?

Thanks 谢谢

The memory management paradigm in Swift is different from C++ since it inherits the retain-release mechanism (through ARC) from Objective-C. Swift中的内存管理范例与C ++不同,因为它从Objective-C继承了保留释放机制(通过ARC)。 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. 正如您所料,C ++的解决方案对编译器的责任较小,更具表现力和优化,但使用起来也更复杂。

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. 所以,回答你的问题: strong (默认)基本上像shared_ptr一样, weak就像weak_ptrunique_ptr没有直接的等价物。 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) 但是,如果编译器能够保证指针的唯一性,那么一些强大的变量可能就像unique_ptr一样(例如,你创建并销毁同一范围内的对象 - 比如函数的主体 - 而不将其分配给任何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). Swift应用引用的ARC(自动引用计数)来决定何时释放引用类型实例使用的内存(即,当该对象的强引用数为零时)。 ARC and its reference counting runs automatically, but holds similiarities to the explicit use of C++'s std::shared_ptr ; ARC及其引用计数自动运行,但与C ++的std::shared_ptr的显式使用有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). 后者将允许(通过共享ptrs)指向的对象仅在指向对象的所有智能指针已超出范围或已明确重置(/无效)时被销毁(例如,通过提供的deletor)。

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. 在上面的例子中,您可以将强不可变(引用) foobar (后者从foobar()返回)视为std::smart_ptr的C ++等价物:它们都指向相同的Foo对象,并且仅当两个都超出范围将对象取消初始化。

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类似的语义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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