Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

The State of the Language: An Interview with Bjarne Stroustrup : Page 2

C++ founding father assesses the language on the eve of its new standard.


advertisement

Concepts are probably the most important addition to C++ in years. Can you please explain which problems in contemporary C++ concepts will solve?

I suspect that what is most important depends on what you consider important. For many, concepts will be invisible. They will simply be "the magic" behind a radical improvement of the error messages you get when you make a mistake using something that happens to be a template. Basically, you'll get the kind of error messages that you are used to from "ordinary functions" and at the time you get them for mistakes with ordinary functions. What's so great about that? I can imagine a time a few years into the future where people won't be able to think of a good answer to that question. This will be the ultimate proof of the success of concepts.

"Concepts" is a type system for types, combinations of types, and combinations of types and integers. Basically, it allows the programmer to state in code what we currently state in documentation and comments. For example, the standard algorithm takes a pair of forward iterators and a value; given that, it assigns the value to each element of the sequence defined by the iterators. That algorithm's requirements on its argument types can be expressed like this:




template<Forward_iterator For, class V>
	requires Assignable<For::value_type,V>
void fill(For first, For last, const V& v);   //  just a declaration, not definition

We can try using fill():


int i = 0;
int j = 9;
fill(i, j, 99);	// error: int is not a Forward_iterator

int* p= &v[0];
int* q = &v[9];
fill(p, q, 99);	// ok: int* is a Forward_iterator

The checking of use happens immediately at the call site and uses only the declaration. This is a major practical advantage over the current state of affairs for templates where the checking happens late—during template instantiation time.

Concepts also allow us to check the definition of a template in isolations from its arguments:


template<Forward_iterator For, class V>
	requires Assignable<For::value_type,V>
void fill(For first, For last, const V& v)
{
	while (first!=last) {
		*first = v;
		first=first+1;	// error: + not defined for Forward_iterator
				// (instead: use ++first)
	}
}

Again, the error is immediately caught at the point in the definition where it happens.

This checking is the most obvious benefit of concepts, but they also give greatly improved specification of algorithms and class templates and makes overloading straightforward, eliminating the complicated scaffolding of traits and helper functions that people have to rely on today.

Most importantly, concepts help us think about our algorithms and their requirements on their (template) arguments. This leads to more reliable and more general code. Often, such thinking also leads to improved performance as we are forced to think about what the algorithm really needs and what is extraneous.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap