C++11 upgrade techniques [closed] - c++

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'd like to begin transitioning to use C++11 in a large, cross-platform codebase. The main problem is that the level of C++11 support varies between the compilers that are used.
Aside from littering macros throughout the code, does anyone have any examples/suggestions on how to ease this transition? Please provide techniques for specific features. For example:
// cpp11compat.h
// For compilers that do not have 'nullptr', we will define it as 'NULL'.
// Any misuses of 'nullptr' will be caught by C++11 compliant compilers.
// Obviously, this won't fix anything that depends on the type 'nullptr_t'
//
#ifdef STUPID_SUN_COMPILER
#define nullptr NULL
#endif
Thoughts?

I would suggest that you start with finding the largest common denominator of C++11 features by current compilers. For a survey see: http://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport
The most widely supported features include auto, decltype and move semantics, so you could start with rewriting your code with those, and make a #define fix for the platforms that don't support a specific feature that most other platforms do support (e.g. nullptr).
For the file organization of all the config files, you might want to check the source code of Boost.Config It contains several directories, ordered by platform and purpose. E.g. config/platform/irix.hpp contains all the platform specific stuff for Irix.
EDIT:
Another good way to do emulation of language features is to look at the Boost libraries for Language Feature Emulation tags. They contain libraries such as Move, Foreach and previously Lambda that have very similar (although not necessarily exactly the same) syntax as the standard C++11 features. This allows you to let Boost worry about platform compatibility.

Little time has passed since the publication of the standard. While you could probably reach almost all of C++11 features through boost, this year probably is not the best timing for starting that type of migration unless you already are on this road, because it would result in migrating twice.
So I suggest that you give compiler vendors another year or two before you start migrating. Meanwhile, try to stop supporting any abandoned platforms where no updated compilers are likely to appear if you feel that C++11 is very important for you throughout the code base.
(In fact, it is completely fine and common to never completely migrate a large codebase to the next version of a language standard, but that is not relevant to your question the way it is currently phrased.)

Related

Should I update my program from C to modern C or C++? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I have an old program written in C from the 1990's. I would like to update it so that it will work for the next persons and on modern compilers. Currently I am using a really old version of TurboC to make changes. Should I be focusing on rewriting this in modern C or C++? Which will be the easiest to bring this code up to date without having to rewrite too much and be able to reuse most of the existing code?
My programming background has mostly been in hprogramming languages like Perl, Python, PHP, Powershell, and Visual Basic, so I am not too familiar with the differences between C and C++.
C and C++ are different languages, not different versions of the same language. Stick to C, although you could use the fancy features of newer versions of the standard, like C99.
Your last sentence pretty much answers your sentiment. If you aren't familiar with the differences and you only know older languages I wouldn't update this program unless there is something horribly wrong with it that is affecting many users.
You could update it just within the C world to adhere to C99 or C11 if you have a newer compiler
Given:
have an old program written in C from the 1990's.
You have two questions:
Should I be focusing on rewriting this in modern C?
Maybe, maybe not. I would try to adhere to a standard C89, C99, or C11. This mainly depends on your tools and how much new development will happen.
Do you like declaring variables other than at the beginning of scope? If so, then possibly update to C99. Are you using any tools that really like C89 and show errors or warnings with C99 conventions? If so, then stick to C89.
If the program is continually being updated and you are hiring young people, then newer conventions might be beneficial.
Should I be focusing on rewriting this in C++?
No.
Most well-written C programs are also valid C++ programs, or require just a little adaptation. The opposite is not true.
It's probably easier to stick with ANSI/ISO C and leave both doors open for the next maintainer.
It's probably about the same effort to move to a modern C compiler vs. a modern C++ compiler. The evolution of C and C++ have diverged where each has similar features but they're not source compatible.
There are a few factors that would lead me to choose to update to C++:
Modern C doesn't seem to enjoy as much support as C++. For example many new things in C seem to get implemented only as required by C++, especially in the Microsoft world. VS doesn't even support C99 except for what's in C++, let alone C11.
C++ is a better C: "C++ is "a better C" in the sense that it supports the styles of programming done using C with better type checking and more notational support (without loss of efficiency)." This is still true of modern versions of C.
C++ adds features that support some very powerful techniques. Really making use of them may be best left to library developers, but that means C++ can support really great libraries.

c++11 threading vs .Net threading? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Which one should I use in my C++11/CLI program?
Which is the pros and which is the cons of c++11 threading and .net threading?
My program is windows only and is built with .net.
Considering that managed languages usually prefer developer productivity over performance (when in conflict), I'd say that managed threading is likely to be more developer friendly. Also, Garbage Collection is a well-known productivity feature.
Do you have an extreme need for performance and/or control? If not, I recommend managed code and managed threading.
C++11 would be a standard based, platform independent way of going about threading. This is important if you need to work across platforms.
Threading with .Net will typically tie your application with the OS (Windows). This has some advantages such as garbage collection (which is amiss in standard C++). However, note that you will probably need to ship the .Net framework with your application. Issues across versions of the framework are not too uncommon.
If you are asking about managed vs. native, then use managed code C# as much as possible, it's a much nicer developer experience all around. Also, the .NET 4 supports excellent APIs to help with multithreading, see Task Parallel Library (TPL) and the concurrent collections support, for starters.
The general purpose advice is to use managed code as much as possible, then use interop to native only as needed, for problems that don't quite fit the managed solution.
If you are asking about using C# vs. C++/CLI, then use C#. Both are CLI (managed) languages, but C++/CLI is more difficult to work with and is best used for some managed/native boundary scenarios. Also, C++/CLI doesn't support Intellisense (at least for VS2010, maybe now supported in 2012?)
Consider tenure: .NET threading has been around for a long time and is known to work with C++/CLI. C++11 threading is new, and I could totally see problems arising between that and C++/CLI since the /CLI stuff isn't as mainstream and it's likely that few other people are using /CLI and C++11 threading together.

Developing cross-platform C++11 code [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
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.
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.
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.
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.
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)).
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
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.

Using C++11 in a production environment with GCC [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
C++11 provides us with a lot of new great and immensly useful tools. GCC support of C++11 has already made good progress. So I have thought about when to switch to C++11. This question relates to gcc only, I do not expect to compile my (our) code with any other compiler.
Would you (did you) switch to C++11 before gcc supports the entire C++11 standard to benefit from the features already implemented? Would you still do this in a production environment where stability and correctness is very important? Do you think it would be a reasonable approach to allow developers only to use certain C++11 features?
How would you (do you) go about deciding when GCCs C++11 support is ready for a production environment?
(Note: I'm aware of this question, but it specifically relates to gcc 4.4 and is somewhat outdated)
It depends.
If it were to power my blog or something like this ? Definitely.
If it were to power a critical service ? Of course not.
I believe that the support of C++11 is too immature as it is now, to be called production ready.
You may settle on a version of gcc, but the truth is that because the successive drafts evolved as new problems were discovered and tackled, the code you write now may well be rejected by a later version, or the behavior may change lightly.
Therefore, I think this judgement truly depends on what you intend to be doing. There is a reason the space shuttle is powered by an old and proven technology: it's a matter of trade-off between ease of development and confidence in the tools.
It's your judgment, you know your situation better than we do.
The GCC C++ developers still think their C++03 support is not up to par, and therefore aren't even setting the __cplusplus version correctly (citation needed, I can look up the bug+discussion). They marked the support as experimental because they started implementing the basics before there was a final draft/standard. By now (ie GCC 4.6), most major flaws have been removed, although some details remain inconsistent with the exact standard wording.
If possible, you should also test with Clang, which IMHO strives and succeeds at better adhering to the puny details in most places where GCC lacks the necessary enforcement. Production use is something that's personal. Me, I think that every compiler has bugs, and although the chance of a bug in the "new stuff" is statistically more probably, chances are you'll also encounter an older bug messing with your perfectly compliant code. That's why I suggest using at least two compilers to prevent any incompatibilities (or at least reduce them as much as possible).
As for the Standard library, libstdc++ is functional for the most part, but lacking in some large and useful parts like <regex>, which is sad. If you're feeling lucky, you should be able to get LLVM's libc++ working on at least Linux and Mac, this is a feature complete c++11 library minus <atomic>), but also the "new kid on the block".
To summarize: the more compilers and Standard libraries you run your code against the better (although you should check which ones are correct, and which are buggy). This inevitably reduces the amount of C++11 features available to you, although if you go with GCC/Clang, only lambda's, uniform initializers and <atomic> fall outside your scope. MSVC is a different story...

C++ or C++0x - Which is a better standard? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 12 years ago.
So I've been trying to do some research and would like the opinions of other developers on this topic. I am an experienced C++ programmer and have been using the current C++ standard for some time. I have been reading articles that "C++0x will undoubtedly become the new standard." How far off are we does everyone think from making the switch to a whole new programming standard? Also, which, in your eyes, is a better standard? From how I understand it, C++0x will come with more standard libraries making development easier without many more dependencies. Please help me to catch up!
Thanks!
Dennis M.
It would be pretty sad if the next version of C++ were quantitatively worse than the current one. The entire point of the new revision is to improve things.
Well, it depends.
The current C++ standard (C++03) is currently "better" because most of the latest C++ compilers and standard library implementations conform fairly well to the standard. Yes, there are issues, but most of them are very well known (e.g., hardly any compiler supports export) or are fairly easy to work around.
Support for C++0x is pretty patchy right now. Different compilers support different parts and there have been pretty major modifications made to it over the last year, so compilers that did provide early support for some features are now "buggy" if you consider their conformance to the latest drafts.
Going forward, though, C++0x will be a huge improvement over C++03. Major features like the concurrency memory model and the standard threads and atomics libraries are extremely important for the future of the language. Move semantics will make it easier to write clean, high performance code. Most of the new language features will make developing in C++ a more enjoyable experience.
"C++0x will undoubtedly become the new standard" is an understatement. C++0x is the draft of the new standard. Parts of it are available now in compilers like G++ 4.5.
It is impossible for C++0x to be qualitatively worse than the current C++ standard, because one of the essential things about the new standard is that it is fully backward compatible. If there are bad parts, you can just avoid them. (Of course, that doesn't mean that new features in C++0x can't be used to create really bad code that you'll have to deal with, but if you're coding on your own, you can always choose to avoid C++0x features that are worse in your opinion.)
Depends on what you mean by "better". If you mean "More likely to work with whichever compiler I'm using at the moment", then the old standard will certainly be better, with a little boost thrown in.