Linked by RandomGuy on Wed 10th Jun 2009 20:00 UTC
General Development This series is aimed at programming language aficionados. There's a lot of very abstract writing about programming languages and a lot of simple minded "language X sux!" style blog posts by people who know next to nothing about programming. What I feel is sorely missing is a kind of article that deliberately sacrifices the last 10% of precision that make the theoretical articles dry and long winded but still makes a point and discusses the various trade offs involved. This series is meant to fill that void and hopefully start a lot of discussions that are more enlightening than the articles themselves. I will point out some parallels in different parts of computing that I haven't seen mentioned as well as analyze some well known rules of thumb and link to interesting blogs and articles.
Thread beginning with comment 367775
To read all comments associated with this story, please click here.
comments on C++
by jacquouille on Wed 10th Jun 2009 20:37 UTC
jacquouille
Member since:
2006-01-02

Quote from near the end of the article:


Still it doesn't feel nearly as wrong as writing templates in C++:

<oh compiler <please forgive me> but I <your puny human user> am unfortunately unable <to tell you <the exact type of this> > >

I'm sure a lot of people will disagree but to me all this fuss just to express the type equivalent of a shrug is ridiculous. The effort should be roughly proportional to the amount of information you want to convey and if hello world needs more than one line the language designer probably didn't understand this or designed his language with only big programs in mind.


There are two wrong things implied here.

The first is that you need to use C++ templates (or more generaly some advanced c++ concepts) if you want to just write a c++ "Hello world".

This is wrong, in C++ exactly like in C, a "hello world" app will consist of 3 lines:

#include <YourFavoriteLibrary>
int main()
{
your_favorite_function("hello world");
}

See, no templates. Intentionally I made this example library-agnostic to emphasize that if anything you'd be really discussing the standard library, not the language itself.

Even if you use the standard C++ library (which starts making your hello world a not-representative use case as most real-world apps use other libraries for I/O), there still are no templates involved. It would be

#include <iostream>
int main()
{
std::cout << "hello world" << std::endl;
}

So, still no templates around. It's true that "cout" is an object, not a function, which is harder for a beginner to wrap his mind around, but the benefits are massive enough to justify that (but we're getting offtopic).

The other part of your comment is about templates syntax:

all this fuss just to express the type equivalent of a shrug is ridiculous.

A template is an undetermined type A that depends on parameters B,C,D,... and the c++ syntax for it is just:

A<B,C,D,...>

so i'm not sure what your critique is. And if you want to make a shortcut for a template notation that you use repeatedly, typedefs are here for that.

Also do notice that templates are an aspect of C++ that:
1) You don't need to touch if you don't want it, and
2) Basically no other programming language has an equivalent of, so C++ can hardly be blamed for at least offering it. No, C macros don't allow 0.1% of what templates allow, neither do Java generics, nor do recent Fortran polymorphism improvements.

So what are C++ templates? Basically they are trees of types. Think of A<B,C> as a tree with leaves B,C atop a common trunk A. Then you can produce any tree, with any types as labels, by nesting templates: like A<B,C<D,E> >. Then people realized that c++ allows to perform any operation (turing complete) on these trees at compilation time. This is what makes c++ so uniquely powerful, but few people understand it.

Reply Score: 7

RE: comments on C++
by ringham on Wed 10th Jun 2009 20:41 in reply to "comments on C++"
ringham Member since:
2006-03-23

Great comment!

Reply Parent Score: 1

RE: comments on C++
by wanderingk88 on Wed 10th Jun 2009 21:04 in reply to "comments on C++"
wanderingk88 Member since:
2008-06-26

And besides, really, statically typed languages provide such a benefit in both small and large-scale projects that dismissing it as a "hindrance" is rather silly.

Sure, there are lots of reasons to prefer dynamic typing, but there are lots of shortcomings to that, too. As long as you're ready to deal with them...

I, for one, hope the day I have to maintain a truly large scale Python project never comes.

Reply Parent Score: 2

RE: comments on C++
by RandomGuy on Wed 10th Jun 2009 21:13 in reply to "comments on C++"
RandomGuy Member since:
2006-07-30

Yeah, that's a good explanation of templates.
The compile time mechanics of C++ are even Turing complete IIRC. The problem is that you _have_ to tell the compiler all those things, even if you don't care and don't want to care.

Also I was using hello world loosely to refer to smallish programs.

Reply Parent Score: 2

RE: comments on C++
by jrincayc on Thu 11th Jun 2009 02:04 in reply to "comments on C++"
jrincayc Member since:
2007-07-24

AFAIK, Ada generics are about as powerful as C++'s. I will take ML and variants such as Ocaml's parametric polymorphism over C++'s generics any day of the week. I do agree that C++'s templates are better than Java's, Fortran's and C's.

Reply Parent Score: 1

RE[2]: comments on C++
by boldingd on Thu 11th Jun 2009 17:09 in reply to "RE: comments on C++"
boldingd Member since:
2009-02-19

AFAIK, Ada generics are about as powerful as C++'s. I will take ML and variants such as Ocaml's parametric polymorphism over C++'s generics any day of the week. I do agree that C++'s templates are better than Java's, Fortran's and C's.


I agree completely: I mostly used C in college. So far, for my job, I've used Ada, C++ and Java. I far, far prefer Ada two either of the other two. Ada is much, much more clear and readable than C++ is. C++ can get nasty: when all those class, angle-brackets, asterisks and ampersands start flying around, it can can become all but impossible to figure out what the hell's going on in a C++ program. I love Ada's simple, rigid, highly explicit typing system - because you can look at it and tell almost instantly exactly what's going on, exactly what everything's type is, and what you're doing to it.

(I really, really hate C++: I could rant about how convoluted, illegible and... generally horrible that language is for a very long time.)

Reply Parent Score: 2

RE: comments on C++
by Bill Shooter of Bul on Thu 11th Jun 2009 05:51 in reply to "comments on C++"
Bill Shooter of Bul Member since:
2006-07-14

The first thing you thought he implied is ridiculous. There is no way he implied that. It was just a random rant against the readability of templates. I know they're powerful and awesome, but used / abused liberally they kill readability.

Reply Parent Score: 3

RE: comments on C++
by yorthen on Fri 12th Jun 2009 19:32 in reply to "comments on C++"
yorthen Member since:
2005-07-06


#include <iostream>
int main()
{
std::cout << "hello world" << std::endl;
}

So, still no templates around. It's true that "cout" is an object, not a function, which is harder for a beginner to wrap his mind around, but the benefits are massive enough to justify that (but we're getting offtopic).


It's true that cout is an object and not a function. What is not true is that there are no templates in the above example. The << operator is actually a template parametrised by the argument (the "Hello World" string).

Yes, I'm nitpicking, but I'm actually trying to make a point: templates can be quite tricky to code, but they are usually quite easy to use.

Reply Parent Score: 1

RE[2]: comments on C++
by FealDorf on Fri 12th Jun 2009 19:50 in reply to "RE: comments on C++"
FealDorf Member since:
2008-01-07

If I'm not mistaken they're overloaded functions, not templates...

Reply Parent Score: 1