c-plusplus.org

  • Increase font size
  • Default font size
  • Decrease font size
The C++ Programming Language

C++11 is now official

E-mail Print PDF

As of 12th August, the new C++ standard, named C++11, has been approved by the ISO, and published yesterday, 11th September 2011.  This was codenamed C++0x for years; a working title, the '0x' referring the year when the standard would make it.  In 2007/8 when I was more active with the standards committees, Bjarne Stroustrup used to joke that 0x would become 0A, so it really should be 0B but obviously they stuck to decimal instead of hexidecimal.   Just a few highlights include:  lambdas, r-value references, threading support, garbage collection support and a whole heap more.  For more detail see:

http://en.wikipedia.org/wiki/C%2B%2B11

and

http://www.open-std.org/jtc1/sc22/wg21/

and

http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=50372

Last Updated on Monday, 12 September 2011 21:47
 

The C++0x Auto Keyword

E-mail Print PDF

As you may know, C++0x has taken the old, defunct auto keyword and completely repurposed it to perform a useful task. Forget what auto did before - it's the default behaviour or variables anyway, so it's unnecessary. Focus on it's new meaning.

So what does auto do now? Well you can declare a variable with the auto keyword instead of a type specifier such as int or double or std::string. Then, the compiler deduces the type of this variable from its initializer.

So, auto x = 4 would make x an integer, as the compiler deduces its type from the type of 4. Note, therefore that a variable declared with auto must have an initializer. if it doesn't, you will get a compiler error, as it cannot deduce the variable's type.

Clearly, this can ease development somewhat, but we must now discuss the right and wrong circumstances in which auto should be used. You should not use auto when the variable type is obvious. This will just result in messy, hard-to-read code, and it doesn't really save any time - int is shorter than auto!

A good circumstance to use auto is one in which writing out the type would be time consuming, or poorly maintainable, for example for an STL iterator.

This is how we'd normally do things: 

std::map addressBook;
for  (std::map::iterator itr = addressBook.begin(); itr != addressBook.end(); itr++)
   std::cout

As you can see, writing down the iterator is a pain. What if we did this: 

std::map addressBook;
for  (auto itr = addressBook.begin(); itr != addressBook.end(); itr++)
   std::cout  

The compiler deduces the type of itr from the type of addressBook.begin(), and suddenly it wasn't so difficult to write the iterator. Also, if we later change the template arguments of the map, we don't have to update the iterators, since the compiler will now deduce the new type for them.

If you want to read more about auto, you can see Dr. Stroustrup's FAQ, or read my blog post on it (where I have a full code example). 

Last Updated on Tuesday, 19 July 2011 11:41
 

Favourite C++ IDE Poll Results

E-mail Print PDF


The most popular C++ IDE is Visual Studio by a long margin.  It was very close, but quite surprisingly vi was second (I was expecting emacs to be second).  I wonder how many people use emacs and vi on Windows?

Here are the final results.  266 people voted in total over a period of about six months:

 Visual Studio 
 112 votes  42.1%
 vi 34 votes 12.8%
 Emacs 32 votes 12.0%
 Eclipse
 29 votes 10.9%
 Bloodshed Dev 27 votes 10.2%
 Kdevelop 21 votes 7.9%
 All of the above
 11 votes 4.1%

 

 

Last Updated on Saturday, 20 February 2010 18:13
 

Polls

What should be added to C++Org next?
 

Who's Online

We have 16 guests online