He didn’t mean any particular language - he meant a language that is as yet unwritten.
C++ is a pretty small language; however, it has some weird rules because of situations that hadn’t occurred to Stroustrup at the time he wrote it. And it did end up with a few more keywords than he put in the original.
The future is not one language, IMO. The future is the ability to mix languages seamlessly, allowing you to pick the exact right language for any part of a project.
Hmmm, he surely has a point. I think Java and C# are neat, their VMs are cool, and IMO the proposed support for generics in C# is, even if no one likes to admit it (a Microsoft language and all), quite “revolutionary”. No other statically typed language has * runtime * support for generics and combines heterogeneous and homogeneous translation schemes. That´s quite a feat; Java developers considered someting similar, but rejected it for good reasons (backward compability). Java generics is a total mess, but still workable.
But back to the original question: In my experience, a lot of people/companies use a rather small but sane subset of C++. I still think C is a very good programming language; it´s easy and efficient. My dream language would be C with local functions and a very simple subtyping system and an inheritance mechanism that is totally decoupled form subtyping. A bit like * Sather *, but more in line with C syntax. And also F-bounded generics; C++ templates went totally overboard, IMO.
I don´t know of any language that does this, however. So sorry if this is OT.
If not, it’s syntax that allows you to write generic code. The simplest example is a C++ max algorithm:
template <typename T> bool GreaterThan( T a, T b )
{
return ( a > b );
}
When you call GreaterThan(3, 5), you get a version of that code for ints. But if you use some other type, you’ll get a version of that code for that type.
The most popular use of generics is for reusable data structures–after all, a binary search tree only cares that its elements are comparable, so why should you have to write a different one for each comparable type?–but there are other, more interesting uses.
I don´t know… I think C++ is a pretty extensive language for what it´s supposed to do. There are hidden traps all over the language (like default assignment operators or copy contructors). It´s not a superset of C (again, there are hidden traps, like the type of an “char” literal or precedence of certain operators). And it´s got a completely different (from inheritance/subtyping) and very powerful abstraction mechanism, namely templates, “bolted” on the side. And RTTI is totally devious. And so on.
It certainly doesn´t help that his Stroustrup´s book is overly verbose and opinionated. When you want some information on a language feature in C, you can grab K&R and look it up. With C++, you have to wade through pages and pages of Stroustrup´s rants to gather that info.
There’s a difference between the language Stroustrup originally wrote and the books he’s written about it. The original language was a superset of C, and was not too large.
The hobby programming that I do on the side has never needed to use templates much from a creation side. I have used plenty of them from the STL, of course, vectors and lists and such, but I’ve never worked on a project that strongly implied the need for making my own templates. My programming experience is only in C++ and Java, so I wasn’t aware that there is some easier way for generic functions. What alternative could exist? They seem pretty generic and concise to me…
I don’t understand all the details, but I think what z_z_z is taking issue with is the fact that C++ templates allow you to put in any damn thing you want. F-bounded generics would have restrictions on them.
Vaguely similar, but essentially different. In your compiled code, the calls GreaterThan(“hello”, “goodbye”) and GreaterThan(15, 14) both execute the same code. In mine, a separate copy of GreaterThan is created inline for each call.
Yes exactly. Since this is the forum for debates: I think C++ templates are too powerful. I know C++ wasn´t designed to force a certain style or paradigm on programmers, and that´s ok. But I still think C++ is not a “small” language, as ** bup ** says; in fact I think it´s vast. Quality compilers that supported all (or even most) of the language features, let alone templates, were not available for a long time. It´s a standard practice to use only a certain subset of the languange for real - world projects.
C++ templates have a lot of problems. They can get extremely complicated, they are translated separately for every concrete binding of a type variable, you need the source code of every generic construct you want to use and usually templates rely heavily on operator overloading, another concept that I don´t like. And they are so powerful that they are a viable alternative to inheritance / subtyping. Where is C++ going?
Have you ever had a look at the STL source code (if you use the STL, you have the source code somewhere on your system)? No doubt it´s very, very clever and I admire the programmers that came up with that code, but it´s so contrived that it makes your head explode.
Personally, I prefer F-bounded generics. It´s not as powerful or expressive as C++ templates, but more in line the with the object-oriented paradigm. ** erislover **, if you know Java, you should look up how Java 1.5 adds generics. A brief intro can be found (as a .pdf) at Oracle Java Technologies | Oracle . That´s F-bounded generics. The implementation details are, no question, absolutely messy, because they really need backwards compability. But I think it´s still better than C++ templates.
Sort of similar. A variant is a data type that can hold a value of any other type. In C#, you can get the same effect with the object type (which is the ultimate ancestor of all types) or interfaces. For example:
public bool GreaterThan(object a, object b)
{
// throws an exception if a and b can't be compared
return ((IComparable)a).CompareTo(b) > 0;
}
Generics in C# are types or functions that can be specialized, in source code or at runtime, to operate on different types. They’re similar to C++ templates, but not the same. Here’s a generic GreaterThan function:
public bool GreaterThan<T>(T a, T b)
where T : IComparable<T>
{
return a.CompareTo(b) > 0;
}
Now you can call GreaterThan(foo, bar) for any values of foo and bar, and the compiler will fill in the type parameter “T” with the actual type. Both parameters to the function must be the same type (since they’re both T), and because of the “where” clause, that type must implement the generic interface IComparable (meaning it can be compared to itself).
You can also have generic types:
public class Pair<T,U>
{
public T First;
public U Second;
}
public interface IComparable<T>
{
int CompareTo(T other);
}
public delegate T EventHandler<T>(object sender, T args);
Unlike templates, generic classes and functions are specialized at runtime, not at compile time. All specializations where T is a reference type will share the same machine code, so GreaterThan<object> and GreaterThan<string> will be exactly the same function at runtime[sup]*[/sup], while GreaterThan<int> and GreaterThan<double> will be unique (int and double are value types).
[sup]*[/sup] Well, they would be if GreaterThan<object> could exist. Any attempt to invoke it will cause a compile time error, because “object” doesn’t implement IComparable<object>.
Run-time specialization is great and all that, but templates are a big deal in scientific computing because they allow for compile-time calculations and the generation of specific code–rather than have a loop to compute a dot product, you can have a sequence of statements to do it.
The trivial example is a factorial function:
template <int N> struct Factorial
{
enum { value = N * Factorial<N - 1>::value };
};
template <> struct Factorial<0>
{
enum { value = 1 };
};
When you invoke Factorial<6>::value in code, that’s replaced with 720 by the compiler.
You could say that again. I never understood why it was distributed with such whitespace-compressed crazy formatting. I always keep a second “human-readable” copy of STL on my hard-drive. I ran this second copy through a code-formatter and I add comments as I explore it.
As to the OP, I agree with bup’s first comment that Stroustrup is probably refering to a language yet unwritten. Maybe it is a simplified C++ that gets back to its preprocessor roots? Perhaps a language without as much run-time support? Maybe even simplier inheritance rules, more focus on interface inheritance / less on implementation inheritance, simplified public/protected/private rules, elimination of function and operator overloading? All these are guesses and each item is a debate in itself.
z_z_z has some great comments about pitfalls. C++ does have a lot of them and it gets a bit irritating to always have to work-around them. I realize it does this to give the user power and flexibilty, but I wish the default behavior was the “common” behavior. I am thinking about things like making your destructors virtual and taming the triumverate of terror: auto-constructor, auto-copy-constructor, and auto-destructor.
I also really agree with ultrafilter’s comment about there being lots of “right” programming languages and being able to pick and mix the best one for a project. OT a bit, but lately I care less about a language’s syntax and more about its standard and semi-standard library support. I hate the idea of solving a problem that has already been well-solved. I think one of the best things about C++ was STL. The idea that there was a standard linked-list already sitting there waiting to be used and (mostly) OS portable. Java takes this concept and pushes it even farther.