2

With C++03 it was (and still is) possible to write cross-platform code with both MSVC and GCC, sharing C++ code bases between Windows, Linux and Mac OS X.

Now, what is the situation with C++11? It seems that different C++ compilers implement different features of C++11. To build cross-platform C++11 code, is it safe to take MSVC10 (VS2010) as a kind of "least common denominator"? i.e. if we restrict the approved C++11 features to those implemented by MSVC10, will the resulting C++11 code be compilable with GCC (and so usable on both Linux and Mac OS X) ?

Or is it just better to wait for C++11 compilers to mature and stick with C++03 if we need cross-platform code?

Thanks.

  • 5
    I'm note sure what kind of answer you expect. Use features all your compilers support, don't use the ones they don't. This is not specific to C++11, happens every time a new standard appears. Alternatively, use the same compiler everywhere and stop worrying about that. – Mat Jan 14 '12 at 12:28
  • No compiler supports all of C++11, so the short answer is, you cannot do it at all. – Kerrek SB Jan 14 '12 at 13:40

6 Answers6

2

You can compile code for Windows using GCC. You don't need to use Microsoft's compiler.

If you want to use C++11 features painlessly at the moment, that's going to be your best solution. Microsoft still has yet to implement a lot of C++11, and not all of it is slated to be in VS11, either.

Otherwise, yes, you can obviously just use the subset of the C++11 features that are supported by the compiler implementation that represents the lowest-common-denominator. You'll need to check and make sure that that is Microsoft's compiler for all of the new features rather than just assuming that it is.
I don't believe GCC has gotten around to everything yet, and there's no guarantee that their implementation of all the features is perfect and matches Microsoft's 100%. Writing completely portable code is and has always been hard.

Using only C++03 features is obviously the safe approach, but it doesn't allow you to use C++11 features (obviously). Rather or not that's important is a decision that only you can make.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • It's not so hard, but you have to abstract the platform differences. It's very manageable to keep a C++ app cross-platform. Of course if you go into the C++2011 territory, your compiler needs to support it. – piotr Jan 14 '12 at 12:37
  • @piotr: Yes, I wasn't talking about cross *platform* code. I was talking about *portable* code that will compile on any compiler. You can't abstract away features that your compiler doesn't support. – Cody Gray - on strike Jan 14 '12 at 12:52
1

C++11 is not ready for prime time yet, as you already figured out.

Not only is the parsing stage still being worked out by the various compilers, but there is also the issue that some, while appearing to accept some features, may have quirks and bugs in the releases you currently have.

The only sound approach I can think of is to first select the compilers you want to use:

  • you can use gcc/Clang on Windows (with libstdc++) however this will prevent you from interacting with libraries compiled by VC++
  • you can on the other hand validate your code for both gcc/Clang and VC++ (and perhaps a few others if you need to)

Once you have determined the compilers you want to use, you then have to pick the features of C++11 that you want to use, and that work on all those compilers.

  • gcc is probably the more advanced here
  • Clang does not have lambdas, but has move semantics and variadic templates
  • VC++ is the most behind I think

And you need to setup a test suite with all those compilers, and on all the platforms you target, and be especially wary of possible code generation issues. I recommend using Valgrind on Linux par example and perhaps Purify (or equivalent) on Windows as they both help spotting those runtime issues.

Beware that both VC++ and g++ may have extensions accepted by default that are not standard, and may also base their interpretation of the code on previous drafts of C++11.

Honestly, for production use, I think this is still a bit wonky.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • Thanks. I think that move semantics (which can give performance improvements), nullptr, auto and lambdas are a "safe" subset of C++11 supported both in MSVC and C++; so I think selecting these features should be OK for production code. I'm not sure how the new C++11 libraries are implemented in both compilers (it seems to me that MSVC is behind GCC from a language perspective, but is in good shape for libraries). –  Jan 15 '12 at 19:37
  • @Mr_C64: Actually, lambdas are not supported by Clang yet. Also, I don't know how threading support is in any of the 3 compilers (and thus how `std::atomic<>` fares), though of course it only concerns multi-threaded code :) – Matthieu M. Jan 15 '12 at 19:51
0

If you are writing new code, you are probably not releasing it tomorrow.

So plan for your release date. There are some features, that will be accepted more slowly than the rest. Mostly hard to implemented features and duplicated features (like the range for loop).

I wouldn't worry much about using the new library features, those are already supported very well across all compilers.

Currently there isn't any least common denominator, since Microsoft decided to concentrate on the library first, while the rest has gone (mostly) for language features.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
0

Visual studio support for C++2011 is quite good, so if you use GCC 4.7 and VS2010 you will be able to use an ample set of the most interesting features of C++2011 while being cross platform.

Support for C++11 overview for VC10 and VC11 http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx

Table for all the compilers: https://wiki.apache.org/stdcxx/C++0xCompilerSupport

GCC C++11 support: http://gcc.gnu.org/projects/cxx0x.html

Also related: C++11 features in Visual Studio 2012

Community
  • 1
  • 1
piotr
  • 5,657
  • 1
  • 35
  • 60
0

This depends largely on your project. If you ship only binaries you need to figure out a toolset you want to use and stick to what this toolset supports. Should your team use different tools everbody should make sure his code builds with the common build system (Be it C++03 or C++11).

The situation changes as soon as you ship headers that contain more than just declarations. First of all you need some infrastructure do determine what is supported by which compiler. You can either write those tests yourself and integrate them with your build system or stick to Boost.Config. Than you can ifdef platform dependent code. This sounds simple at first but really isn't. Everytime you have C++11 code that can be implemented with C++03 work-arounds you want to have both versions available for your users (e.g. variadic templates vs. the preprocessor). This leads to duplicated code and comes with a significant maintenance cost. I usually only include C++11 code if it provides a clear benefit over the workaround (better compiler error messages (variadic templates vs. macros), better performance (move semantics)).

pmr
  • 58,701
  • 10
  • 113
  • 156
0

Use only those features of C++11 at the moment which improve your code in some manner. Let me explain this, I don't look up C++11 features to use them, rather when they solve my problem I adopt them. (This is the way I learned about them, all on SO) This approach will change in future, but for now I am doing this.

I currently use only few features of c++11, which incidentally work in both VS2010 and GCC. Also if there is a great feature, you want to use, and VS doesn't have it, why not use GCC. It is cross-platform, so will work on windows as well.

Vinayak Garg
  • 6,518
  • 10
  • 53
  • 80