1

Have an Objective-C++ function. (The file has the correct .mm extension).

int SPGetNumericAttribute(NSMutableString* line, NSString* &attr) {...}

Some code calls as follows:

NSString* queryAttr = nil;
int res = SPGetNameAttribute(line, queryAttr); <-- error

Compiler complains:

No matching function call for 'SPGetNameAttribute'.

Is there a technical reason why you can't pass an Objective-C object to a C++ reference parameter? My guess is something to do with ARC.

3
  • so first you show declaration of SPGetNumericAttribute function and then you show how you call SPGetNameAttribute function. Which one is correct? Commented Jan 25, 2018 at 4:31
  • SPGetNameAttribute() declaration compiles without any errors. Calling SPGetNameAttribute() and the compiler complains. You can declare a reference to an NSObject*, but can't pass one. Why? Commented Jan 25, 2018 at 19:12
  • Please show the declaration of SPGetNameAttribute. I want to see what this function look like. What you show now is declaration of a different function named SPGetNumericAttribute Commented Jan 25, 2018 at 19:18

1 Answer 1

0

ARC needs to know how to handle the reference counting of the second parameter inside of the function. By default the parameter has autoreleasing type. Thus your variable queryAttr must have the autoreleasing ARC type

__autoreleasing NSString* queryAttr = nil;

or alternatively you can declare your function as

int SPGetNumericAttribute(NSMutableString* line, NSString* __strong &attr) { ... }

and the error disappears for strong variables. Tests with Instruments show that ARC seems to be handling this right, too. But then you may only use strong variables for this parameter.

I think it should be better, if you use a pointer instead:

int SPGetNumericAttribute(NSMutableString* line, NSString** attr) { ... }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.