Disscussion on a C++ 2011

New C*+ standard have many features that can simplify usage of cocos2d-x. Since C*+ 2011 support is almost complete on android, iOS, Mac OS X and recent Linux distributions, can we discuss it?

There are also perfomance boost from rvalue refs, but since cocos2d-x uses raw pointers, I don’t expect noticeable effect (may be rvalues can increase perfomance for smart pointers and geometry classes, since return-value optimization can be impossible for cocos2d-x compiled as shared library or as static library without LTO in compiler settings).

Non-static member initialization.

CC_SYNTHESIZE/CC_PROPERTY creates property that should be also explicitly initialized in constructor. With non-static member initialization it can be changed to initialize value automatically: just expand macro to int memberVar = 0;. There are two cons:
* variable still keeps garbage for compilers without C++ 2011 support (GCC before 4.7 and Visual Studio).
* not all types can be initialized with zero. Problem can be eliminated by GCC/clang extension which allows to determine macro parameter type.

std::function

Pros:
* it simplifies async operations (i.e. all code for several threads can be written inside one function, like in Objective-C with blocks).
* it simplifies delayed operations, usually performed by CCSequence which contains CCDelayTime and CCCallFunc.

Cons:
* std::function available only on modern compilers versions, so cocos2d-x should hide such features under macro like CC_COMPILER_HAS_LAMBDAS and user code becomes not so portable (to Windows).

foreach loop for CCArray * and maybe CCDictionary/CCSet.

Pros:
* Can be achieved with simple adaptor class: https://github.com/sergey-shambir/cocos2d-x-extensions-cpp2011

Cons:
* should be hidden under macro CC_COMPILER_HAS_RANGE_FOR

Initializer lists

Pros:
* For data structures, can replace create(CCAction **, …), so user will never forget to add NULL at end.
Cons:
** should be hidden under macro CC_COMPILER_HAS_INITIALIZER_LISTS

override and final attributes

Pros:
* Can be wrapped by macro CC_OVERRIDE and CC_FINAL

unordered_map

Pros:
* can be used inside cocos2d-x to speed up some operations, for example, at weak references counting mechanism.

I’m not that confident in C11 support for Cocos2D-X is good timing.
Especially for Android and BB10.
The Android NDK still has a looong way to go in many areas.
The BB10 toolchain is quite old and I’m not sure it can deal with C
11 properly.

No idea about NaCl and emscripten (being added currently in a PR on GitHub).

Romain TISSERAND wrote:

I’m not that confident in C11 support for Cocos2D-X is good timing.
>
Especially for Android and BB10.
The Android NDK still has a looong way to go in many areas.
The BB10 toolchain is quite old and I’m not sure it can deal with C
11 properly.
>
No idea about NaCl and emscripten (being added currently in a PR on GitHub).

As far as I know, BB10 uses GCC 4.6. Android NDK also defaults to GCC 4.6, but supports 4.7 too. There are no problem with iOS toolchain, since it based on latest clang (and GCC 4.2 is deprecated). Emscripten based on clang, so it supports c++11: http://comments.gmane.org/gmane.comp.compilers.emscripten/980

GCC 4.6 supports all mentioned features except non-static member initialization and override/final, library support works at least on Android. GCC 4.7 and latest clang both support all mentioned features.

I think that real problem is Windows with Visual Studio. Many people use Visual Studio in development, and it’s the only way to compile for windows phone. So even with macro checks code becomes unportable to windows phone and no so handy for debugging in VS.

I was not aware GCC 4.6 had good enough C11 support.
Has Visual Studio such bad C
11 support ?
I never looked into WinRT that much, but it looked to me it was vastly using C11 stuff.
Still, switching Cocos2D-X to C
11 looks like heavy changes to me.
Maybe in a separate branch ? Would make sense when merging WP8 / WinRT code since this will be a major rewrite :slight_smile:

Romain TISSERAND wrote:

I was not aware GCC 4.6 had good enough C11 support.
>
Has Visual Studio such bad C
11 support ?
I never looked into WinRT that much, but it looked to me it was vastly using C11 stuff.
Actually, Visual Studio 2012 supports everithing mentioned except non-static members initializers: http://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport
So you are right, everithing OK with windows phone.
>
Still, switching Cocos2D-X to C
11 looks like heavy changes to me.
Maybe in a separate branch ? Would make sense when merging WP8 / WinRT code since this will be a major rewrite :slight_smile:
It can be done as optional feature, hidden under appropriate macros. In this case, Visual Studio 2008-2010 users also will not hurt.