[M3devel] Enumeration or subrange value out of range

Jay K jay.krell at cornell.edu
Tue Nov 30 09:32:12 CET 2010


> Recently I met these... Isn't it nice, to find your error happening somewhere in some .h file you never heard of?


The error messages are better these days.
It is true they were terrible for a long time.
Don't Modula-3 generics have the same problem?
At least in the C++ case:
  - the errors aren't in generated files 
  - you don't have to explicitly instantiate everything "in the build system"
  - for function templates, you never have to instantiate anything
 The automatic deduction of function template parameters is very powerful.
 You have to try it.
 I *believe* it enables a style of programming similar to ML, esp. when combined with the more recent
 feature "auto".


It is also true that templates seem to be largely accidental in what they allow.
It seems they were designed with two primary applications:
  std::vector
  std::min/max


The compile-time Turing-completeness seems to have been an accident,
and what people use this for is/was perhaps better implemented through special
language constructs builtin to the compiler. It is both impressive and depressive.
Impressive that it is doable, depressive what it looks like.
When people do "meta programming" like "is_pod", "has_copy_constructor", etc.


Consider this though, have you heard the ability to do this:
matrix<int> a,b,c,d;
a = b + c + d;


where there are no intermediate matrices?
The results are written directly into a.


The people do this is that operator+ doesn't return a matrix, but rather
a type that represents the result matrix addition, which is assignable
to a matrix, and can also be added to matrices or the results of matrix addition.
So ultimately what is assigned to a is something representation the addition
of b, c, d. You can add any number of matrices. Or subtract. Or multiply (within
the rules of matrix multiplication) etc. There is no special casing, no special
purpose code to handle certain combinations. I don't believe any other
language has this power.


Simple string addition can benefit from this same technique.
Instead, string concatenation gets very special treatment
by compilers, including I believe in Modula-3, Java, and C#.
That's not "fair". Why should string get all the power and syntactic sugar,
but not any user-defined types?


Look for articles by Todd Veldhuzien. Libraries such as Blitz++.
http://www.cs.rpi.edu/~musser/design/blitz/meta-art.html
http://www.oonumerics.org/blitz/
etc.


If C++ is just a bit too gnarly and difficult to understand and implement,
I suspect subset exists with about the same level of expressiveness. But I don't know.
 

 - Jay

> Subject: Re: [M3devel] Enumeration or subrange value out of range
> From: dragisha at m3w.org
> Date: Tue, 30 Nov 2010 08:49:01 +0100
> CC: hosking at cs.purdue.edu; m3devel at elegosoft.com; kcdurocher at gmail.com
> To: jay.krell at cornell.edu
> 
> Recently I met these... Isn't it nice, to find your error happening somewhere in some .h file you never heard of?
> 
> No, it isn't ! :)
> 
> I don't care much for generics, but templates are just one of C hells. Nice concept, surely, when everything goes well. But once it doesn't, you'll not solve it inspecting it - guessing is what you're doing. And peppering your code with "cerr <<".
> 
> I understand you're doing C/C++ for ages... It is like climbing a hill on the way to your work. After some time, it becomes funny and you (almost) enjoy it. Except for part where you're comimg sweaty to that morning meeting, but well - one can't have everything. I knew people in Belgrade with just such a nice workplace :) - meteorologic observatory on a bit remote hill.
> 
> On Nov 30, 2010, at 4:43 AM, Jay K wrote:
> 
> >   generics are good, but not as good as C++ templates  
> 
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20101130/2b2954dc/attachment-0002.html>


More information about the M3devel mailing list