Linked by RandomGuy on Wed 10th Jun 2009 20:00 UTC
Thread beginning with comment 367775
To read all comments associated with this story, please click here.
To read all comments associated with this story, please click here.
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.
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.
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.)
#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.





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.