Print

C11/C++11: Building blocks for the future



Larry O Brien
Email
August 27, 2012 —  (Page 3 of 4)

C++ changes
Type inference: Nowhere are the lessons of the mainstream languages clearer than in the adoption in C++ of type inference. The clearest visible signature of C++11 code will be the use of the auto keyword on the left-hand side of assignments. While this may feel a bit like the dynamic typing of a Python or Ruby, it’s not: The type is fixed and statically determined. The auto is just a convenience to avoid cumbersome “finger-typing” that often just restates what you’ve typed on the right-hand side (auto instead of, e.g., map<int,list<string>>::iterator or function<void(void)>). This “type inference” will be familiar to users of C# or Scala but will take some getting used to by others (especially when the compiler implementation is unable to infer the correct type and complains about it. Such complaints are a common source of questions on Stack Overflow for type-inferred languages).

Lambdas: Type inference saves a good amount of space when it comes to complex parameterized types, but is also an important enabler of what’s probably the biggest functional improvement to C++11, which is support for lambda functions.

Anonymous inline functions are again familiar to users of some of the more well-designed managed languages, but are even more universal in the world of JavaScript. Such “lambda” functions are wildly useful, particularly when used with Standard Template Library higher-order functions such as find_if. The syntax is a little more cumbersome than what one sees in other languages, as C++ requires you to specify the “captures” when the lambda function is being used as a closure. For example, [foo &bar](bat) -> int { …function body…} captures foo by value, bar by reference, takes bat as a parameter, and returns an integer (the return type can be elided in most cases). While the flexibility for different capture semantics is very nice, one can imagine stumbling the first several times one encounters that type of code.

Range-based for: The third highly visible feature in C++11 is “range-based for.” This compact way of specifying a loop can work with any type that supports the concept of a “range” (essentially, any type that has functions begin and end that yield iterators). All containers, arrays, initializer lists and regex matches can be looped on with a simple for(auto p : ps). This provides remarkable savings over the long type signatures of C++, to which developers using the standard containers have become accustomed.

What’s more likely to change the experience of programming is the stylistic imperative to “always use smart pointers or non-owning raw pointers.” Smart pointers in C++11 use the language’s template mechanism to provide reference-counted memory management, a type of garbage collection that is not as foolproof as the fully automatic garbage collection of managed languages, but which is much easier than fully manual memory management and is compatible with powerful C++ idioms such as Resource Acquisition Is Initialization (RAII).

C++98 had a flawed implementation of reference counting using the auto_ptr type. This type is now deprecated; instead, one uses unique_ptr for the owning relationship, and weak_ptr and shared_ptr for cycles and shared ownership. The great challenge with reference counting is that if you create a cycle in the object graph, the memory will never be freed; in C++11, the programmer is still responsible to correctly build the ownership graph.

Concurrency: C and C++ were invented in the era of single-processor machines, but are still the languages of operating-system implementations for the foreseeable future. As mentioned previously, the C11 memory model has been improved to be multi-thread safe, and C++ builds on that with a number of primarily library-based features.

Now, std::thread runs any callable object and runs it asynchronously. More interesting are the techniques associated with futures, which provide a straightforward conceptual model. (A Future is an IOU for a value; calling get on it blocks until the value is ready.) All the usual caveats (and flashing red lights and dire warnings nailed to doors in the dead of night) about the pitfalls of references and pointers and object lifetimes continue to apply.

The standard additionally specifies several types of Mutex, thread-local data, and thread-safe initialization. It is by no means a silver bullet for concurrency, but instead is an attempt to be a solid foundation for further work.


Related Search Term(s): C, C++

Pages 1 2 3 4 


Share this link: http://sdt.bz/36891
 

close
NEXT ARTICLE
News Briefs: November 15, 2008
Metaforic comes out with a new security product for monitoring networks, ICEsoft and ILOG announce that they are merging their technologies for later releases, and Urbancode adds pre-flight builds for testers to AnthillPro Read More...
 
 
 




News on Monday  more>>
Android Developer News  more>>
SharePoint Tech Report  more>>
Big Data TechReport  more>>

   
 
 

 


Download Current Issue
MAY 2013 PDF ISSUE

Need Back Issues?
DOWNLOAD HERE

Want to subscribe?