Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "the issue" with variable qualifier placement?

In this document, under the section labeled "Variable Qualifiers", Apple says:

You should decorate variables correctly. When using qualifiers in an object variable declaration, the correct format is:

ClassName * qualifier variableName;

for example:

MyClass * __weak myWeakReference;
MyClass * __unsafe_unretained myUnsafeReference;

Other variants are technically incorrect but are “forgiven” by the compiler. To understand the issue, see http://cdecl.org/.

Looking at cdecl.org doesn't clarify anything. Can anyone explain what "the issue" they are referring to is? In other words, help me convince others that this actually matters in a way that isn't just "because this one readme says so."

like image 385
i_am_jorf Avatar asked May 20 '14 17:05

i_am_jorf


1 Answers

See my examples with gibberish to English translations

It is well known that

ClassName * const varName; //varName is a constant pointer to ClassName

has different meaning than

const ClassName * varName; //varName is a pointer to constant ClassName

or

ClassName const * varName; //varName is a pointer to constant ClassName

In the same way this declaration

ClassName * __weak varName; //varName is a weak pointer to ClassName

and this declaration

__weak ClassName * varName; //varName is a pointer to weak?? ClassName??

are VERY different. However, the meaning of the second one is clear (although it's technically incorrect) and it can be "forgiven" by the compiler.

The correctness is a bit more important once you start working with pointers to pointers (e.g. Foo * __autoreleasing *).

I assume they wanted to protect beginner developers from the C/C++ declaration gibberish. Having the qualifier in the beginning seems more natural.

like image 187
Sulthan Avatar answered Sep 30 '22 13:09

Sulthan