-
Notifications
You must be signed in to change notification settings - Fork 7
CPlusPlus
By default (and like Objective-C), Eero supports direct calls to C functions, allowing full access to existing C APIs and libraries.
The latest version of Eero also supports direct C++ interoperability, enabled through a special pragma:
#pragma eero "C++"
This should appear before any code or #import/#include statements. (After comments is ok).
The intent is to provide support for existing C++ APIs/libraries, not to create a new dialect of Eero. There are no major restrictions, but a few special rules and guidelines exist to prevent language conflicts and clutter.
Declaring/defining new C++ classes within Eero source files
C++ class declarations are best kept in separate headers and pulled into Eero source files via the compatibility variants of #import/#include — see Direct #import of Eero and legacy (Objective-C and C/C++) headers. However, you can declare or define C++ classes within Eero source files, with the following restrictions:
-
C++ class member functions cannot have inline definitions; they must be defined outside of the declarations.
-
There is a keyword conflict when using class for forward declarations, but you can still forward declare C++ classes using the struct keyword, since there are no semantic differences between it and C++'s class.
C++ lambdas
C++'s lambda syntax is not supported. However, you can simply use blocks instead, since the compiler implicitly converts them to lambdas:
std::vector<int> v
...
std::for_each( v.begin(), v.end(), ^(int x | cout << x << endl) )
Exceptions
Eero recognizes try and catch as language keywords, so there is no syntactic way to distinguish between Objective-C and C++ exception handlers. However, Eero supports C++ exception handling seamlessly by allowing the mixing of Objective-C and C++ catch statements*:
try
Log('trying...')
// id elem = objcArray[20] // uncomment this line to see objc exception thrown
stlString.substr(1000, 1001) // throws C++ exception
catch std::range_error& e
Log('caught C++ range exception')
catch std::exception& e // handle all other C++ (STL) exceptions
Log('caught C++ exception: %s', e.what())
catch Exception e // handle objc exceptions
Log('Caught %@ => %@', e name, e reason)
catch ... // handle exceptions of any sort not caught previously
Log('caught "...!"')
* Requires appropriate runtime, which includes (but is not necessarily limited to) iOS and 64-bit OS X.
C++11 range-based for loops
These are supported whenever the C++ pragma is in effect (and no C++11 compiler warning will be issued). There is one syntactic difference, however; the in keyword is used instead of the colon between the loop variable declaration and the range object expression:
std::vector<int> v
...
for int i in v
Log( 'Vector item: %d', i )
The compiler knows when the in is followed by an Objective-C object, NSRange, C++ range, or C-array, and produces the appropriate results. Thus this feature is supported without introducing yet another for loop syntax.