Skip to content

Enable C++11 and C11 #2175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

matthijskooijman
Copy link
Collaborator

Now that we have a branch with a more recent gcc version we can enable
C++11 support.

This enables some more powerful and useful C++ features, like variadic
templates, rvalue refs / move constructors (for more efficient code -
String class already supports these), constexpr (to allow e.g. function
calls as constant values), foreach-style loop, enumeration classes,
static assertions. See http://en.wikipedia.org/wiki/C%2B%2B11 for
details.

These features are mostly useful for more advanced users and library
authors, but there is a lot in there that can make a significant
difference to code efficiency or conciseness.

Enabling C++11 should be completely backwards compatible.

@Chris--A
Copy link
Contributor

I agree, I have been using -std=c++11 in 1.5.7 and it has worked fine with the projects I've tested. Did not know about the gnu++11 alternative.

It appears 4.8.1 has every feature of C++11 working except for garbage collection. https://gcc.gnu.org/gcc-4.8/cxx0x_status.html

C++11 makes many things far easier, as you mentioned constexpr, which will allow coding simple functions that once would have required a template.

@cmaglie cmaglie added this to the Release 1.5.8 milestone Jul 15, 2014
@matthijskooijman
Copy link
Collaborator Author

When merging this, #2179 and #2213 should also be merged.

@Salandora
Copy link

Hello,

I might be wrong, but shouldn't you also update the platform.txt in the sam folder?

Greetings
Salandora

@matthijskooijman matthijskooijman added the Component: Compilation Related to compilation of Arduino sketches label Sep 10, 2014
@matthijskooijman
Copy link
Collaborator Author

@Salandora, good point! I've rebased the changes and applied them to sam as well. Thanks for reminding me :-)

Next up: Verify why some of the examples were changed (as in, the resulting .hex file changed) between with and without C++11.

Lauszus added a commit to felis/USB_Host_Shield_2.0 that referenced this pull request Sep 11, 2014
@matthijskooijman
Copy link
Collaborator Author

Ok, I finished compiling all the examples shipped with Arduino, with and without C++11. 55.5% of the compilations resulted in identical hex files. 38% of the compilations resulted in a (slightly) different hex file (for those keeping count, the remaining 6.5% were compilations that failed, with or without C++11).

I had a closer look at the diffs for the builds that were different (I looked over them all, but not too close and every one of them, we're talking about 600 diffs here). I've seen the following differences:

  • Slightly different code layout (e.g the code for a return statement halfway the function with a jump over it, vs return statement at the end with a jump towards it, things like that).
  • Use of the String move constructor in the String examples (this was the only change I saw in SAM code)
  • Use __nltoa_ncheck / __itoa_ncheck instead of nltoa / itoa (optimized away radix validity check). Note sure what in C++11 would enable this optimization, but the result looked ok to me.

Of course, there were some other changes like changed addresses etc, which resulted from the above changes.

I haven't actually ran the examples for testing (too much!) but I've been running most of my sketches with C++11 enabled for more than a year now, so I'm pretty sure things will run as well.

As far as I'm concerned, this PR is done and ready to be merged.

@ffissore
Copy link
Contributor

@ArduinoBot build this please

@matthijskooijman
Copy link
Collaborator Author

I just rebased this against latest ide-1.5.x. Is anything keeping this from being merged?

@ArduinoBot
Copy link
Contributor

Can one of the admins verify this patch?

@matthijskooijman
Copy link
Collaborator Author

@cmaglie, could you put this on the schedule for 1.6.1? Having C++11 support really opens up a lot of (optimization) opportunities, at little to know cost. It's a fairly trivial change, too.

@ffissore
Copy link
Contributor

@ArduinoBot build this please

@cmaglie cmaglie modified the milestones: Release 1.6.0, Release 1.6.1 Feb 18, 2015
This does not change anything, it just makes the defaults explicit.
This uses the gnu++11 standard, which is C++11 with GNU extensions.
C++11 should be full compatible with the previously used C++98
standards, so all pre-existing sketches should continue to work.
Gcc 4.8 defines __cplusplus as 201103L, so we can check for that now. It
still also defines __GXX_EXPERIMENTAL_CXX0X__, but this could help on
other compilers, or if gcc ever decides to stop defining the
experimental macro.
This uses the gnu11 standard, which is C11 with GNU extensions.
Previously, gnu89 was being used, which is pretty ancient by now. C99
brings some important improvements, some of which were already available
and used even without this option. C11 is more recent and brings more
minor improvements. Most notable feature is the static_assert statement,
allowing checking invariants at compiletime using the full C
expressions.
@matthijskooijman
Copy link
Collaborator Author

I've just rebased this PR and re-ran the tests. Now, more examples compile to identical results than before, 82.6%. Not sure what changed there, though. A quick glance over the resulting assembly shows the same changes as described before.

@matthijskooijman
Copy link
Collaborator Author

@ffissore ffissore modified the milestones: Release 1.6.5, Release 1.6.6 Jun 15, 2015
@ffissore
Copy link
Contributor

@ArduinoBot build this please

@matthijskooijman matthijskooijman changed the title Enable C++11 Enable C++11 and C11 Jun 15, 2015
@matthijskooijman
Copy link
Collaborator Author

On addition: Previously, this PR enabled C++11 and made the C99 default explicit. However, I've found out that the default for C is actually C89, so this PR actually changed the C standard used (which I think is a good thing). The latest version of this PR now also enables C11, the most recent version of the C standard (which, compared to C99, hasn't seen a lot of changes, just a few small additions of which static_assert is probably the most useful one).

@matthijskooijman
Copy link
Collaborator Author

I just found another reason for wanting C++11: delegating constructors, where a constructor can call other constructors in the same class.

Is there anything blocking this? It's fairly trivial change, and more and more code seems to benefit from it.

@Chris--A
Copy link
Contributor

Chris--A commented Jul 7, 2015

There are numerous reasons I can vouch for activating C++11.
Personally I can see some great simplifications for newbies by using foreach constructs (#2179) and I think user defined literals have a lot of potential with embedded style code.

@cmaglie
Copy link
Member

cmaglie commented Jul 16, 2015

Rebased and merged.
Thanks for the meaningful discussion, and sorry for delay!

@cmaglie cmaglie closed this Jul 16, 2015
cmaglie added a commit to cmaglie/ArduinoCore-samd that referenced this pull request Jul 17, 2015
See arduino/Arduino#2175

Quoting the original commits from Matthijs Kooijman:

    Enable C++11 support

    This uses the gnu++11 standard, which is C++11 with GNU extensions.
    C++11 should be full compatible with the previously used C++98
    standards, so all pre-existing sketches should continue to work.

    Enable C11 support

    This uses the gnu11 standard, which is C11 with GNU extensions.
    Previously, gnu89 was being used, which is pretty ancient by now. C99
    brings some important improvements, some of which were already available
    and used even without this option. C11 is more recent and brings more
    minor improvements. Most notable feature is the static_assert statement,
    allowing checking invariants at compiletime using the full C
    expressions.
@matthijskooijman matthijskooijman deleted the ide-1.5.x-c++11 branch August 18, 2015 11:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Compilation Related to compilation of Arduino sketches Component: IDE The Arduino IDE Component: Toolchain The tools used for compilation and uploading to Arduino boards feature request A request to make an enhancement (not a bug fix)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants