C++ appeals to serious writers of useful libraries, and users of those libraries.
There's nothing freaky here. The compiler can't know, by itself, that a template argument must
model a numeric type. Concepts allow the library writer to say so. Then, when a user tries to
use the template on something that isn't numeric, the compiler can say so right there, instead
of complaining that some expression down in the function body is ill-formed.
As noted above, macros in C have the same problem, but hardly anybody tries to do anything
ambitious with C macros; far fewer do it successfully, in part because of precisely this sort
of problem. In essence, a C++ library can extend the compiler, and compiler extensions need
their own error messages. Concepts gives library writers a practical way to provide them, to
describe the error in terms of the library interface, not in terms of details of the library
implementation, which is all a compiler could conceivably do by itself.
Posted Aug 7, 2009 19:50 UTC (Fri) by hummassa (subscriber, #307)
[Link]
> The compiler can't know, by itself, that a template argument must model a numeric type.
Sure it can. The compiler has access to the source of the template; it will have to generate code based on that source eventually, so it knows what it needs.
Even if the compiler does not "plan ahead", during the instantiation phase it can deduce WHY the instantiation went wrong, instead of spilling WHAT went wrong with it... and point to the right lines of code, and giving meaningful class names instead of "complete" class names (IOW:
std::string
instead of
std::basic_string<char, std::char_traits<char>, std::allocator<char>>