Let's say I have an Objective-C class Foo:
@interface Foo: NSObject
@end
@implementation Foo
@end
Then in an objective-C++ file (.mm) I have following code:
Foo *strongFoo = [Foo new];
__weak Foo* weakFoo = strongFoo;
auto foo = weakFoo; // ???
What is the type of the variable foo on the line containing "???" ? Is it a strong or a weak pointer to Foo? Is this defined somewhere in some standard and/or documentation? Thanks.
I based on a rule to make a summary:
1. Objective-C++ does not change rules of C++ and back.
auto
works differ then typeof
declaration. auto
works similarly templates.
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
In the clang docs you can find description of Ownership Qualification in Template arguments part, you can read:
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.
There is good example in the description of auto
of a template function to determine type of a variable:
For example, given
const auto& i = expr;
, the type ofi
is exactly the type of the argumentu
in an imaginary templatetemplate<class U> void f(const U& u)
if the function callf(expr)
was compiled.
As you can see a ownership qualification of const U& is not declared. Therefore U
is a strong reference to Foo
.
In your example auto foo = weakFoo;
will convert to template<const (Foo*)> f(const Foo*& u)
. As you can see Foo* is a strong reference to a Foo instance.