在 Objective-C++ 中应用于 __weak 指针时,"auto" 关键字推导类型的规则是什么?

What are the rules for deduced type by "auto" keyword when applied in Objective-C++ to __weak pointer?

假设我有一个 Objective-C class Foo:

@interface Foo: NSObject
@end
@implementation Foo
@end

然后在 objective-C++ 文件 (.mm) 中,我有以下代码:

Foo *strongFoo = [Foo new];
__weak Foo* weakFoo = strongFoo;

auto foo = weakFoo; // ???

包含“???”的行中的变量 foo 的类型是什么? ?它是指向 Foo 的强指针还是弱指针?这是在某些标准 and/or 文档的某处定义的吗?谢谢。

我根据一个规则做一个总结:
1. Objective-C++ 不改变 C++ 规则并返回。

autotypeof 声明不同。 auto works 类似模板。

Once the type of the initialiser has been determined, the compiler determines the type that will replace the keyword auto using the rules for template argument deduction from a function call

在 clang 文档中你可以找到 Ownership Qualification in Template arguments 部分的描述,你可以阅读:

If a template argument for a template type parameter is an retainable object owner type that does not have an explicit ownership qualifier, it is adjusted to have __strong qualification. This adjustment occurs regardless of whether the template argument was deduced or explicitly specified.

判断变量类型的模板函数autodescription中有很好的例子:

For example, given const auto& i = expr;, the type of i is exactly the type of the argument u in an imaginary template template<class U> void f(const U& u) if the function call f(expr) was compiled.

如您所见,未声明 const U& 的所有权资格。因此 U 是对 Foo.
的强引用 在您的示例中,auto foo = weakFoo; 将转换为 template<const (Foo*)> f(const Foo*& u)。如您所见, Foo* 是对 Foo 实例的强引用。