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.
Permalink for 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.