DCSIMG
C++ 0x, Will you save C++? - Pavel's Blog
Sign in | Join | Help

Pavel's Blog

Pavel is a software guy that is interested in almost everything
software related... way too much for too little time

C++ 0x, Will you save C++?

The emerging new standard of C++ (dubbed C++0X, where X was supposed to be a decimal digit, but now can be considered a hexadecimal digit) will probably be finalized and approved this year (probably to become “C++11”), and is supposed to march C++ into a new era of productivity. Will that actually happen?

In recent years, I saw declining usage of C++ in “regular”, data driven applications, UI and graphic applications, in lie of other environments, namely .NET and Java. As I do a lot of training, the number of C++ or advanced C++ classes I’ve taught has dropped dramatically, from at least once a month of C++/advanced C++/COM/DCOM fully booked class about 8 years ago, to about a single(!) C++/advanced C++ class a year! C++ has largely been replaced by .NET and Java. Although this observation is somewhat personal, this is also evident in my consulting jobs – most of them in the .NET arena (in the Microsoft world) as opposed to the native C++ world.

Bjarne Stroustrup , the inventor and original implementer of C++ states in one lecture he’d given on C++ 0x, that one consideration in developing the new standard is to make it easier on beginners to get a good grasp of C++. Unfortunately, I find it to be quite the opposite. C++ is complex as it is, adding more intricate features cannot simply make it easier. Although features like auto, enum class, “for each” and nullptr should make things easier, or at least, more understandable, other features will make things harder, especially for those coming from a C background. Features, such as lambda functions, rvalue references, decltype and variadic templates (to name a few) will make it harder for beginners to step into C++ – they will go for C#/.NET or Java unless they really have no choice. And I would have to agree with them - C++ has some complex features in the pre-C++ 0X standard – and it’s going to get worse.

The last C++ standard came out in 1998 (with slight unimportant modifications in 2003). That’s more than 12 years - almost an eternity in “computer time”. C++ simply fell behind. And the language itself is not the only issue. A more important issue is the standard library.

Once upon a time, C++’s standard library seemed well and good, made clever use of templates and seemed rich enough. Today it’s not even close to what other platforms offer. For example, C++ has no threading support, no database access support and no XML support. Some would say database support is too specific, and should not really be in a standard library. What about the other two examples? Sure, there are many libraries out there that give all that and more (most notably the boost libraries), but this should really be a core of the languages support libraries. The new C++0x standard makes up for much of those, adding support for threading, for instance.

What’s important in a language, really?

A computer language is not just a sum of the keywords and some syntactic rules; the other part is its accompanying libraries – they are the first key to productivity. But is that all? I believe that a third factor has emerged in recent years, becoming more important than even the standard libraries – tools.

To be productive you need tools. Sure, you can create any program using Notepad or (heaven forbid – vi), run command line compilation, build ridiculously unreadable makefiles (that only you and the compiler can decipher), but it’s not going to be productive for most programmers. You’ll need some more tools such as a debugger. For a language to be successful, it must have good tools, and productivity is everything.

In the Microsoft world (of which I’m part as a Visual C++ MVP at least), the main work tool is Visual Studio, which continues to grow in features, most of them geared towards .NET developers. I must admit, when I start a new project, I use .NET/C# unless there is a compelling reason to use C++ (such as writing a device driver or doing low level Win32 work). I am not “ashamed” of this, even though I’ve used C++ for the most part of my professional life – .NET is simply more productive. Using C# is a pleasure, because of tooling support and the language itself. Advanced C++ programmers sometimes take pride in devising clever template meta-programming code, but I see this as an exercise in cleverness, not in productivity.

Where is C++ going?

The next statement may be harsh, but I think C++ is destined to become a niche language, good for Real Time code, device drivers, and the like. There’s simply no good reason (in the long run) to continue to use it for “standard” applications with UI, databases, graphics and the like. Mobile platforms don’t use (for the most part). It simply provides no real advantage. The .NET and Java libraries are huge and cover almost everything one may need, the tools are pretty good and getting better, and the safety features of C#/Java is a must. In C# you cannot corrupt memory, you cannot leak memory (at least not in the long run) – unless you use “unsafe” code or native code. In a pure managed environment you cannot do real damage. I think this is not a luxury, but a requirement for good quality code. I’m not saying it’s not possible to create quality code using C++ – I’m just saying it’s more difficult, as the programmer needs to take more things into consideration and worry about things like low level memory management. If you’re thinking COM (in Microsoft’s world), with its reference counting strategy, or things like shared_ptr<>, etc., they both have issues (e.g. cyclic references), and in any case they are not the “default”, so require a conscious effort to use.

In today’s powerful hardware some things should be moved to the platform level. Just as most programmers don’t use assembly language on a day to day basis – although a good assembly programmer can create faster code than the best compiler – it’s taken for granted that the benefits of a high level language are more important that squeezing a few more machine cycles. I believe this is the same in relation to native/managed code. The benefits of managed code outweigh the performance and memory footprint penalties that are inherent in such environments, and these penalties will be reduced with time, with better compilers and better hardware, just as it did with C/C++ vs. assembly.

What about kernel code?

An area where C/C++ is currently dominant is in an OS kernel. Whether it’s Windows, Mac or Linux, the kernel is written mostly in C (with some C++ and some assembly). It seems this this area will continue to be dominated by C/C++. But does it have to?

I’ve written some device drivers in the past, and it’s a pain. Granted, some people really love it, but even they can’t claim it’s easy. And any exception is brutally unforgiving, giving (on Windows) the infamous Blue Screen Of Death (BSOD). the BSOD is not a Microsoft directed punishment, as some naturally assume, but a mechanism to save Windows from not booting again. If a kernel code would be allowed to continue running, critical files may be damaged or parts of the registry corrupted, so the safest route is to stop everything. This means driver writers need to be extra careful, as they can cause a system crash, not just crash a specific process. Microsoft mandates various tests for drivers to ensure stability and quality, making it even harder on drivers writers. But does it really have to be that hard?

The Singularity project from Microsoft Research is an interesting attempt to create an operating system in managed code. Imagine: you write drivers in C#! You get all the safety and comfort of a managed environment within the kernel! Although there are naturally issues to resolve, I believe this is the way of the future. There’s really no good reason not to write most of an OS kernel in a managed environment, be it .NET, Java or whatever.

Conclusion

C++ is still pretty widely used in the game industry, for example. It was always a given that games need top performance, squeezing every once from the platform. The existence of libraries such as Microsoft’s XNA (and other managed game engines), that can be used to produce respectable games on the PC and even XBox 360 prove that you don’t really have to use a native language. In a world where video card’s GPUs are key and multi core is everywhere - not every single machine cycle has to count.

Managed environments are the way of the future (and the present). It’s a bit sad that noble languages like C++ suffer as a result, but that’s more a nostalgic vision than anything else. Sometimes we simply must let go, even when it’s painful.

That’s my two cents.

Comments List

# re: C++ 0x, Will you save C++?

Published at Saturday, February 05, 2011 8:53 PM by eladkatz  

I agree with basically everything that you're saying.. but there's one thing that baffles me.

You pointed out games and OSs which are of course the weak-spot of managed programming languages, but it's not the only issue.

MS Office, which you'd think would be the best example for a program which would be a nightmare to write in C++, and pure joy to write in WPF/C# is not written in C#. not v2007, and not v2010. And you've got to wonder why.

More than that. If you try and think of MS progams written in WPF/C# you'll find that it's not at all that easy. Sure, we've got VS2010 and Blend, but I'm not sure those really count - can we name programs outside of the .net community?

Makes you wonder, doesn't it?

# re: C++ 0x, Will you save C++?

Published at Saturday, February 05, 2011 10:21 PM by pavely  

You're right - there's still a way to go...

MS office has a lot of codebase from the days when no .NET existed, so it's not that easy to port (and costly). Other than that, there are some "political" issues when talking about Office...

VS 2010 is written mostly in managed code, with WPF being the UI layer. But even VS 2010 has lots of native code as expected from the need to support so many diverse environments.

There are definitely more non-Microsoft software written in .NET than in MS itself, but I see this changing too. New MS products are written in .NET.

# re: C++ 0x, Will you save C++?

Published at Sunday, February 06, 2011 11:25 AM by Sasha Goldshtein  

@Elad: You can implement 99% of the user-mode parts of Windows in C# or any other managed language of choice. But if you're looking at approximately 50 million lines of code, I'm sure you can understand why anyone would be reluctant to undertake something like that. Same goes for Office, and many other Microsoft products.

And Visual Studio 2010? I don't have accurate statistics, but I'm willing to bet that 80% of the code is still C++.

This is a transition that's going to take MUCH longer than one could anticipate, even if you put together a team of 5000 developers who will make it their goal to rewrite every single Microsoft product from scratch in managed languages.

# re: C++ 0x, Will you save C++?

Published at Monday, March 14, 2011 8:25 PM by bubblecyber  

I see c++0x as the new 'assembly language' to which higher level language should compile to. This would allow insertion of c/c++ code as was done for __asm{ .... } to maintain compatibility for a while. It would also allow using the existing compilers to crerate native code and maintain speed.

In summary:

'NEWLANGUAGE++' --->  C++/C code ---> native.exe

I think the way to go.

# re: C++ 0x, Will you save C++?

Published at Wednesday, April 27, 2011 5:49 PM by RealisticMAN  

You are only talking in Microsoft Windows terms. Remember, there isn't only Windows.. And there isn't only .NET (or Java). Linux, xBDS, MacOSX use C/C++ and Python. Embedded platforms use C! The .NET runtime in written in C/C++

# re: C++ 0x, Will you save C++?

Published at Wednesday, April 27, 2011 7:31 PM by pavely  

I'm using Microsoft based technologies as an example only. This applies to other environments.

I'm not saying C++ is useless; rather, there are better alternatives today. That is most evident in the .NET/Java domain.

# re: C++ 0x, Will you save C++?

Published at Thursday, April 28, 2011 11:03 AM by RealisticMAN  

Yes, Better alternatives, but only for some software types.

Some C/C++ abilities:

- True multiplatform (you only have to abstract OS calls)

- Doesn't need a big runtime environment (good for embedded platforms)

- True obfuscation due to compilation in machine code (you can't revert code in its original form, assembler code is too complex to revert a full program)

- Very good performance, intended as speed / memory requirement (if propely programmed)

- Maximum control over low level details

- Interoperability with all other programming languages (at least exporting functions in C way, if there isn't a better alternative like C++/CLI).

I know that C++ has also some illogical in its design (illogical if you don't consider its forced C compatibility) but with experience and solid rules (defined starting a project), you cannot see so much difficulty compared to C# and Java (C++ template code in specific cases is easier than C# generics).

.Net is very good for its big and solid library but Windows only!!!

Actually I write multipurpose libraries in C++.. Finally integrate these in other language (usually .NET in Windows, Python in Linux)

Regards

# re: C++ 0x, Will you save C++?

Published at Tuesday, May 17, 2011 7:53 PM by Michael Earls  

".Net is very good for its big and solid library but Windows only!!!"

You can develop .NET (C#) code for Linux or Mac using Mono.

http://www.mono-project.com

# re: C++ 0x, Will you save C++?

Published at Monday, June 13, 2011 2:25 PM by patlecat  

There have always been 2 camps (e.g. C against C++, Java against C++, etc.). So this is the "against C++"-Camp. Unfortunately your blog message is not adding anything to the discussion but the usual opinions we all have.

There's a reason why almost all the big software companies use C/C++ instead of C# (even Microsoft). Think about it...

# re: C++ 0x, Will you save C++?

Published at Tuesday, June 14, 2011 6:03 AM by Aleksey  

I agree with you on many points you made. At the same time it seems that some parts of your comment demonstrate the lack relevant experience: say in ~50m LOCs sphere: (1) emacs + makefiles do rule supreme still, (2) platform independence is simply a matter of survival, if you need extensive platform adaptations your company will be out of business sooner than you can possibly adapt to the new platform requirements.

(2) makes using  C# and VsXXXX counterproductive (regardless of any relative virtues the have), and lives c++, java and scala. Each of whom (I agree with you completely) suck big time on their own but have a virtue of being cross-platform.

# re: C++ 0x, Will you save C++?

Published at Tuesday, June 28, 2011 4:31 PM by RealisticMAN  

@Michael Earls

I know Mono, I have seen also its source code (library and runtime) many many times. But I don't consider it. It is only an effort of a single company, it isn't supported directly by Microsoft. Why use crap like Mono when you have community supported alternatives??? Use .Net on windows when your business is 100% windows but change programming language / development platform if your business is Linux / BSD / MAC. C# is a good lnguage, but .Net isn't only C#. All in all, for every job use the right tool. Better to know many tools. The better programmer is who know how learn in little time and adapt himself to the problem to solve.

Regards

# re: C++ 0x, Will you save C++?

Published at Wednesday, July 13, 2011 5:09 PM by Mikhail Semenov  

Just to expand the comment made by Michael Earls. If you want cross-platform GUI,

you can develop in .NET and then run it on Linux using Mono. C/C++ is good when you need speed. Just write your critical code in C/C++, create a DLL/shared object and call it from C#. Works on Windows and Linux. (You have to compile your C++ code separately on Windows and Linux, though).  

I have developed TCP/IP, multithreading applications in C#: exceptions are great things to catch upredictable communication errors. It's great.

# re: C++ 0x, Will you save C++?

Published at Tuesday, August 16, 2011 5:24 PM by LastDodo  

I would say that with the current libraries and mechanics it is not too  difficult to write stable and rock solid code. Memory corruptions and mem leaks only occur when you start using pointers and go really low level with arrays and stuff. Most problems in native programming are based on that. Nowadays We have smart pointers, really fast and optimised collections and good compilers! Sure, programmers need some brains to write c++ ;) but that is not a bad thing at all. Wirting programms in managed environments makes lazy and computer ressources are still and will be limited!! Java applications eat up lots of memory and are really sluggish, especially when they were inactive over time, due to some garbage collection taking place...

c++ will stay with us for some time, just as C does,  and i recommend everyone who only uses java / c# to take a look at it! It builds character :D and you will learn many great language concepts. Every good programmer should know at least 6 programming languages ;) go ahead and pick yours...

# re: C++ 0x, Will you save C++?

Published at Saturday, December 22, 2012 2:10 AM by Coretta  

Hahah ، تحطمت بلادي كمبيوتر محمول عندما كنت أتصفح beta.blogs.microsoft.co.il آخر مرة كنت هنا. وخلال الشهور الماضية 2 لقد كنت أبحث عن هذا مدونة ، لذلك أنا ممتن يقع مرة أخرى! : D

# re: C++ 0x, Will you save C++?

Published at Thursday, January 31, 2013 2:51 AM by escortgirls  

Daar is natuurlik 'n baie te leer oor hierdie. Ek dink jy het 'n paar goeie punte in Features ook. Hou die werk, goeie werk!

# re: C++ 0x, Will you save C++?

Published at Friday, February 01, 2013 7:01 AM by shipra  

Hello all,I am new and I would like to ask that what are the benefits of sql training,What all topics should be covered in it?

And has anyone studied from this course www.wiziq.com/.../2118-learn-how-to-program-in-c-language of C tutorial online?? or tell me any other guidance...

would really appreciate help

Leave a Comment

(required) 
(
required
)
 
(optional)
(required) 

Enter the numbers above: