Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Random Objective-C++0x questions
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Random Objective-C++0x questions



On Jul 11, 2011, at 12:50 PM, Kelvin Chung wrote:

> I have a few questions relating to combining Objective-C with C++0x (henceforth termed "Objective-C++0x") and specifics to how the two separate languages "mix" with each other.  I'm not sure whether this or cocoa-dev is the best place to ask, but it's worth a shot here.  I'm not particularly interested in debates about using one language over the other (or the paradigms of one language over the paradigms of the other), but I am interested in the pitfalls of trying to (ab)use both at the same time, in the hope of getting a better understanding of using Objective-C++ as a language unto itself rather than a "glue language", so to speak.
>
> 1. Is it possible to declare an Objective-C selector with the C++0x trailing return type?  Something like this perhpas?
> - (auto) doSomethingWith:(id) anObj -> id;

No. (FWIW, I can't think of any case where you would want to.)


> 2. Is there a unified foreach loop that will work for both the C++ and Objective-C contexts, since both foreach loops have different syntaxes?  Are the two interchangeable?

No.


> 3. Is nil synonymous with 0 or nullptr?  If I had foo(int) and foo(id), which one is called if foo(nil) is executed?

nil is identical to NULL. nil is not identical to 0 or nullptr. If you like nullptr, it should work fine in all Objective-C contexts.


> 4. Are there plans to add an Objective-C++0x specific category to Cocoa foundation classes, or a C++ STL add-on to deal with Objective-C types?  For example
> + (NSArray*) arrayWithTempSTLVector(std::vector<id>&& vector);
> + (NSArray*) arrayWithInitializerList:(std::initializer_list<id> items);
> - (std::shared_ptr<NSArray>) sharedPtrFromSelf;

No.


> 5. I've heard that while id is a typedef for objc_object*, you cannot use objc_object as a template parameter.  Is this true?  Am I really not allowed to do something like the constructions below?  Can I treat Objective-C classes as if they are "derived classes" (in the C++ sense) from objc_object, which must be declared Objective-C style?
> typedef boost::intrusive_ptr<objc_object> intrusive_id;
> template <class ObjCClass>
> HomogeneousNSArray<ObjCClass> : public boost::intrusive_ptr<NSArray> {
>    // which is implicitly an extension of intrusive_id
>    explicit operator std::vector<ObjCClass>() { /* ... */ }
> };
> HomogeneousNSArray<NSNumber> arrayOfNumbers;

No. In most cases you can use NSObject instead, I think.


> 6. Can a smart pointer to an Objective-C class (like boost::intrusive_ptr) use Objective-C property syntax?  Without having to, say, subclass the smart pointer and insert a whole lot of "return get().property;"?

No.


> 7. Is it possible to automatically convert between an Objective-C block and a C++ lambda expression?

No.


> 8. Does constexpr work with Objective-C objects at all.

No. All Objective-C methods are virtual, so a method cannot be declared as constexpr.


> 9. Do Objective-C classes still have to conform to the C++03 definition of POD, or can they be relaxed to the C++0x definition?  Do C++ class members still have to be POD, or are they relaxed? (Just default-constructible?  Just assignable?  Fully-parameterized templates?  Virtual inheritance?  Virtual functions?)

Objective-C ivars of C++ types may be non-POD. Object allocation calls the ivar's default in-place constructor. Object deallocation calls the ivar's destructor. (This has been true since Mac OS X 10.3 or so, though some compiler versions required an extra flag to enable it.)


--
Greg Parker     email@hidden     Runtime Wrangler


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Random Objective-C++0x questions (From: Kelvin Chung <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.