Posted: Mar 21, 2012 4:18 pm
by mizvekov
VazScep wrote:@[color=#CC0000][b]mizvekov[/b][/color] Do you have any thoughts about "concepts" in C++?

Well I mentioned them briefly a few posts back. :grin:

They were supposed to have gotten in with the latest standard, but they were scrapped because of one issue that popped up, and they didn't want to delay the whole shebang just because of that. Now, that issue has been solved since, and I expect them to be up again for the next standard, possibly to be released in five years.

Now, their purpose is to specify constraints on the types templates can accept. Currently, the constraints are specified by the code itself, and failure to match happens by compilation error at the offending piece. This leads to the infamous longish (to put it mildly) compiler error messages when you pass a nonsensical type to a template. :yuk:

Now concepts allow you to specify the constraints of a type beforehand. What kind of operations it supports, what functions can you call it with, which members it has, and others. So the next time you try to instantiate a template with an incompatible type, the error will be much more comprehensible, specific and hopefully smaller.

Now it also allows for some optimization opportunities, since you can also use concepts to specify specializations for templates.
For example, you could define a special implementation scheme of a generic class that makes use of some special feature of one of the possible type parameters, and also later define a more general implementation scheme which works with any types.

Now one feature that I find *very* cool about all of this is that the concept proposal also includes something else which I didn't mention, something called axioms.
You can use them to further specify some semantic properties of concepts. For example, you could use it to specify that some operation is commutative:
Code: Select all
axiom Commutativity(Op op, T x, T y)  {  op(x, y) == op(y, x);  }

And this allows for some kinds of optimizations which were just not allowed under any circumstances before, as per the standard.