Subj : Re: Compiler and an interpreter To : comp.programming From : Jon Harrop Date : Mon Aug 01 2005 07:11 pm Rob Thorpe wrote: > What Jon's written I can't entirely understand, except that he's using > many of the most complex features of C++ at once. Yes. Specifically, I tried to use template partial specialisation to improve the performance of numerical code that I wrote during my PhD in computational physics. Although the idea was sound, it turned out to be infeasible. There were huge discrepancies between compilers (on some seemingly simple things, like the type of "++") that meant that I had to choose a compiler. I chose GCC. GCC turned out to be full of bugs (v 2.95) and never managed to compile my code correctly (often segfaulting itself). > It isn't really Lisp written in C++, this would be done differently in > Lisp. I think Gerry is getting at the idea of using functional composition via templates in C++. I wasn't doing that. I had tried to do it before but, although the compilers were happy with the code, I found the syntax so unwieldy that I never got my head around it. For example, computing the maximum of 5 and each element of a list in OCaml: # List.map (max 5) [1;2;3;4;5;6;7;8;9];; - : int list = [5; 5; 5; 5; 5; 6; 7; 8; 9] and in C++: #include #include #include #include using namespace std; int mymax(int i, int j) { return max(i, j); } int main() { list l; for (int i=1; i<10; i++) l.push_back(i); transform(l.begin(), l.end(), l.begin(), bind1st(ptr_fun(mymax), 5)); copy(l.begin(), l.end(), ostream_iterator(cout, "\n")); return 0; } IMHO, the OCaml is far more comprehensible. > It depends very much on your point of view whether the code Jon posted > is idiomatic C++. A lot of people know C++, but only know some areas > of the language. Yes. I've never used exceptions, for example. > Often different programmers know different areas of > the C++ language and write in different dialects of it. > > As far as I can tell there aren't many norms amongst C++ programmers > for what idiomatic C++ is. Some programmers use classes and the STL > and steer clear of everything else. I've met a few people (on the net) who avoid the STL at all costs. Other people have standardised Boost, but that was created after I moved from C++ to OCaml. > Stroustrup treats C++ as a multi-paradigm language, where it is > idiomatic/conventional to use the best/most concise paradigm for > expressing what you're doing. By this measure what Jon wrote probably > is idiomatic. > > Personally, I don't think it's very pleasant though, I'd rewrite it in > a simpler way even if it required more code. Yes, towards the end of my C++ programming I had stopped abusing templates (it just doesn't work) and ditched all of the pseudo-functional aspects of the STL. -- Dr Jon D Harrop, Flying Frog Consultancy http://www.ffconsultancy.com .