Languages - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Languages arrow Page 3 - C++09: A Glimpse into the Future
C++09: A Glimpse into the Future
By Danny Kalev

Rate This Article: Add This Article To:

C++09: A Glimpse into the Future - ' Type Concepts '
( Page 3 of 3 )

Type Concepts

Ever since their introduction, C++ templates have suffered from one significant limitation, namely the lack of a mechanism for defining and enforcing constraints on templates. Common workarounds, such as tag dispatching and type traits are inherently complex, verbose and don't guarantee early detection of constraint violations. In some cases, there's simply no way to enforce a constraint at compile time.

ADVERTISEMENT

Douglas Gregor and Bjarne Stroustrup's concepts proposal puts an end to this embarrassment. The concepts proposal introduces a separate type-checking system for templates. Template declarations are augmented with a set of requirements known as a concept. When a template is instantiated, the compiler checks whether the instantiation complies with the concept's requirements.

Consider the std::min() algorithm:

template<typename T>
const T& min(const T& x, const T& y) 
{
 return x < y? x : y;
}

The user has to examine the body of min() to know what the requirements from T are. In this case, T must be a type that has an operator< taking two references to T and returning a Boolean result. With concepts, this requirement can be expressed as follows:

auto concept LessThanComparable 
{
 bool operator<(T, T);
};

The definition of min() uses this concept as follows:

template<LessThanComparable T> //T must be a LessThanComparable type
const T& min(const T& x, const T& y) 
{
 return x < y? x : y;
}

Henceforth, whenever the compiler encounters an instantiation of min, it checks whether T meets the LessThanComparable requirements before generating the code for that instantiation. If T isn't LessThanComparable a descriptive error message will be issued.

Removing Tiny Annoyances from C++

C++09 includes a few core language changes that remove inelegance and minor annoyances.

The angle brackets proposal removes a long standing syntactic displeasure, i.e., the requirement that the right angle brackets of a nested template argument shall be separated with spaces:

//C++98
vector <list <int>> vli; //compilation error; >> treated as a right shift operator
vector <list <int> > vli; //OK

The rationale behind this C++98 restriction was the "maximal munch" rule. However, current C++ compilers are smart enough to figure out that the last >> belong to two templates, so there's no reason to burden programmers with this restriction anymore. (It is legal to use spaces in C++09, though.)

nullptr is another simple yet welcome addition to C++. Many programming languages have a specific keyword that designates a null pointer or reference. C++ uses the literal 0 for this purpose. This hack causes ambiguity problems, particularly in overload resolution. The proposed nullptr solves this problem:

void f(char *);
void f(int);
f(nullptr); //calls func (char*) because nullptr isn't int
f(0); //calls func(int) 

Delegating constructors (also known as forwarding constructors) will make C# and Java newcomers feel at home when using C++09. A delegating constructor uses a member initializer list that invokes another constructor (called the target constructor) of the same class. Once the target constructor returns, the delegating constructor may perform additional operations. This way, common initialization operations are encapsulated in one constructor of the class, instead of being repeated in every constructor:

struct S
{
private:
 S() {cout<<"target ctor"<<endl;} #1
public:
 S(bool b): S() {cout<<"delegating ctor"<<endl;} //invokes target ctor first
};
S obj(false);

The output shows the constructor execution order:

target ctor
delegating ctor

The Road Ahead

Additional C++09 features include, among the rest:

  • extern templates (not to be confused with exported templates) which is a mechanism for suppressing the implicit instantiation of templates
  • C99 long long type and its unsigned counterpart
  • C99 __func__ predefined identifier
  • constexpr, a generalized mechanism for treating a function calls as constants
  • template aliases: a generalized typedef facility that can be used for templates
  • Operator decltype for querying the type of an expression

At this stage, it isn't clear yet whether all of these proposals will make it into the next standard. While there is a clear intention to do so, the voting on the Final Candidate Document must take place before the end of 2007 to comply with ISO rules. This leaves the committee two more meetings (in April and October 2007) to finalize the list of C++09 features. C++09 is much closer than it seems!



 
 
>>> More Languages Articles          >>> More By Danny Kalev
 



Microsoft's Future: A Chat With Their CTO, Barry Briggs

Play Video >

All Videos >

Julia explores the Robotics Studio!

Read now >

Messages to Bill Gates!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.