Overview: C++ Gets an Overhaul

Overview: C++ Gets an Overhaul

en years after the ratification of the first ISO C++ standard, C++ is heading for no less than a revolution. C++0x, the new C++ standard due in 2009, brings a new spirit and new flesh into the software development world. Brace yourself for state-of-the-art design idioms, even better performance, and a plethora of new features such as multithreading, concepts, hash table, rvalue references, smarter smart pointers, and new algorithms. No doubt you’ll find a lot to like in C++0x!

New Core Features
The two most important features of C++0x are concepts and concurrency support. Concepts enable programmers to specify constraints on template parameters, thus making generic programming and design immensely simpler and more reliable (see Douglas Gregor’s excellent introduction to concepts). Variadic templates, template aliases (also called template typedefs), and static_assert—though not directly related to concepts—will also make the use of templates in generic libraries more intuitive, flexible, and less error prone.

The importance of a standardized concurrency API in C++ can’t be overstated: As multicore processors are becoming widespread, you simply can’t afford to remain stuck in the single-threaded era, or compromise on platform-dependent APIs. At last, there’s a portable, standardized and efficient multithreading library for C++. To get a glimpse of the new concurrency facilities of C++0x, you’re welcome to read Anthony Williams’ brilliant introduction to multithreading in C++0x. You can find additional info about thread-local storage here.

Rvalue references are yet another silent revolution. While most users will probably not even know they exist (read my interview with Bjarne Stroustrup), rvalue references enable library designers to optimize containers and algorithms by implementing move semanticsand perfect forwarding easily, thus reducing unneeded copy operations.

Automatic type deduction is made possible by the new keywords auto and decltype which deduce the type of an object from its initializer and capture the type of an expression without having to spell it out, respectively. Adding auto and decltype also paves the way for a new function declaration syntax. The function’s return type appears after the ->sign:

auto func(int x)->double {return pow(x);}

Lambda expressions and closures are another prominent feature of C++0x. A lambda expressionis a nameless function defined at the place where it’s called. It is similar to a function object except that the programmer is rid of the burden of declaring a class with a constructor, defining an overloaded () operator and an instantiating a temporary object of that class—this tedium now becomes the compiler’s job. Here’s an example of a lambda expression:

//a lambda expression is used as an argumentmyfunc([](int x, int y) -> int {return  x+y;} ) 

The lambda expression is indicated by the lambda introducer [] followed by a parameter list in parentheses. The optional return type comes next, following the ->sign. Finally, the lambda block itself is enclosed in braces.

Convenience Features
Some C++0x features are meant to simplify recurring programming tasks and minimize boilerplate code. Most of these “convenience features” were borrowed from other programming languages. These convenience features include:

Listing 1demonstrates these features.

C++0x also removes some embarrassments from the language. For example, C++03 has at least three different forms of initialization:

int x=0;int x(0);int y[2]={1,2};

C++0x defines a unified initialization notation which can even be used in the declaration of dynamically allocated arrays:

int* a = new int[4] {1, 10, 20,95 };vector vs={"ab","cd","ef"};class S { int arr[3];public:  S() : arr{ 1, 2, 3 } {}};

More Control in Your Hands
Certain new core features are meant to provide a standardized and uniform mechanism for controlling the compilation and execution environment programmatically. For example, take memory alignment. In C++03, you have to resort to compiler-dependent, non-portable hacks if you want to override the default alignment of your data. C++0x introduces two new operators: alignof and alignasthat query and override the alignment requirements of your data, respectively:

alignas (16) class Session{ long long timestamp; int authorizations; bool active; //...};cout<< alignof(Session) <

The C++98 rules regarding constant expressions force implementers to use low-tech macros instead of inline functions to ensure compile-time evaluation of constant expressions. This issue, and other limitations of C++98 constant expression rules, have led to the formation of a generalized constant expression mechanism in C++0x. Unlike const, the new constexprkeyword guarantees that the expression it qualifies can be used as a constant expression for example, in an array declaration:

const int y=func(); //dynamic initializationint buff[y]; //error, y isn't a compile-time constantconstexpr int f(int x){  return x * x; } //mandatory compile-time evaluation int arr[f(3)]; //OK, array's size is 9

Generally speaking, constexpr tells the programmer that a certain expression or object is evaluated and initialized at compile-time, as opposed to constwhich merely states that the object in question shall not change its state after initialization.

The onerous problem of undesirable implicit conversions that occur with C++98 conversion operators has also been addressed in C++0x. Up until now, programmers and library writers have had to rely on hacks such as the indirect conversion idiom to reduce the risk of undesirable conversions. In C++0x, you can declare conversion operators as explicit, thus gaining tighter control over the type of conversions that are permitted.

Compatibility with Other Standards
C++0x enhances compatibility with other independent International Standards. The first set of additions is designed to bring the C++ in closer agreement with the ISO/IEC 9899:1999 Standard C (C99 for short). C++0x compatibility with C99 consists of several new header files, among the rest:

  • : The C99 complex arithmetic library
  • : The floating point environment and support for the IEEE-754 floating point arithmetic
  • : The C99 integral types functions and macros

The influence of the recent Unicode 4.0 standard is also reflected in C++0x. C++98 defines a wide char type called wchar_t, that has an implementation-defined size. In the mid-1990s, it was assumed that wchar_t would be sufficient for supporting Unicode but this turned out to be a false hope. The unspecified size of wchar_t prohibits portable UTF encoding in C++98. C++0x solves this problem by introducing two new character types with standardized sizes: char16_t and char32_t that are specifically designed to support portably all the Unicode 4.0 codesets and encoding schemes (UTF8, UTF16 and UTF32). Obviously, some supportfor Unicode strings in its Standard Library will be available too.

The C99 standard also introduced long long for handling 64-bit integers. C++0x fixes this incompatibility at last by adding long long and unsigned long long to its list of fundamental types. Similarly, the C++0x Standard Library defines new long long overloads of the math functions and appropriate std::complexspecializations.

Library Extensions
The number of C++0x Standard Library extensions is quite overwhelming. I will list here only the more prominent extensions.

A new library for manipulating regular expressions is defined in the C++0x header. Regular expression support has been noticeably lacking in C++—especially among web programmers, designers of XML parsers, and other text-processing applications.

Back in 1997, time constraints didn't allow the standards committee to include hash tables in the first C++ standard. The new C++ standard fixes this by adding four new unordered (hash) containers: std::unordered_map, std::unordered_multimap, std::unordered_set, and std::unordered_mutiset.

Perhaps the most popular Standard Library extension is the set of new smart pointer classes: std::tr1::shared_ptr and its little known sibling, std::tr1::weak_ptr.

The class template std::tr1::array wraps a built-in array with a standard container's interface. Similarly, a tuplegroups an arbitrary number of objects of unrelated types in single, container-like object. With respect to algorithms, 11 new algorithms were voted into draft standard in June 2008. Some of these algorithms fill holes in the C++98 standard, whereas others simplify common tasks. Here are some of them:

  • all_of( first, last, pred): This returns true if all elements in the range satisfy the predicate pred
  • any_of( first, last, pred): This returns true if any element in the range satisfies pred
  • copy_n(first, n, result): This copies n elements into the result
  • iota( first, last, value): For each element in the range, this assigns value and pre-increment value by ++value
  • none_of( first, last, pred): This returns true if none of the elements in the range satisfies pred

Finally, the rangeand subrange class templates of C++0x bundle pairs of iterators, thus significantly simplifying certain sequence operations and compacting the signatures of many popular algorithms.

The Road Ahead
The vast number of new features forces the committee to work at an incredible speed. A clear statement of intent was made to complete work on the new standard at the San Francisco meeting of September 2008 in order to achieve publication in 2009. To meet this ambitious timetable the plan is to vote out a feature complete Working Draft at the next meeting. A Final Committee Draft will be issued from the following meeting, allowing at least a minimal time for review and "integration testing" of the new features—particularly a conceptualized Standard Library.

devx-admin

devx-admin

Share the Post:
Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India,

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional

Solar Geoengineering

Did the Overshoot Commission Shoot Down Geoengineering?

The Overshoot Commission has recently released a comprehensive report that discusses the controversial topic of Solar Geoengineering, also known as Solar Radiation Modification (SRM). The Commission’s primary objective is to

Remote Learning

Revolutionizing Remote Learning for Success

School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing pandemic. This major investment, which

Revolutionary SABERS Transforming

SABERS Batteries Transforming Industries

Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs that could dramatically change the

Build a Website

How Much Does It Cost to Build a Website?

Are you wondering how much it costs to build a website? The approximated cost is based on several factors, including which add-ons and platforms you choose. For example, a self-hosted

Battery Investments

Battery Startups Attract Billion-Dollar Investments

In recent times, battery startups have experienced a significant boost in investments, with three businesses obtaining over $1 billion in funding within the last month. French company Verkor amassed $2.1