Lambda the Ultimate

inactiveTopic The New C++: Trip Report, October 2002
started 5/5/2003; 12:16:52 PM - last post 5/10/2003; 11:33:18 AM
Dan Shappir - The New C++: Trip Report, October 2002  blueArrow
5/5/2003; 12:16:52 PM (reads: 3722, responses: 47)
The New C++: Trip Report, October 2002
I'm going to spend this column reporting on happenings at and around the most recent C++ Standards meeting, because things are now heating up, at least a little. In particular, the first two proposals for C++ Standard library extensions have now been accepted by the committee - and it's something of a breakthrough, in that this represents the first new work to be accepted since the first round of standardization was completed in 1997!

Herb Sutter reports on the wild going-ons at the C++ Standards committee (registration may be required to view this article). They are actually considering new extensions to the C++ language!

The many fans of C++ here can find more information at the ISO C++ committee's website.
Posted to OOP by Dan Shappir on 5/5/03; 12:17:20 PM

Ehud Lamm - Re: The New C++: Trip Report, October 2002  blueArrow
5/5/2003; 12:30:22 PM (reads: 2573, responses: 0)
They are actually considering new extensions to the C++ language!. Well, these additions to the library sound interesting:

  • Doug Gregor's proposal for polymorphic function object wrappers.
  • Jaakko Järvi's proposal for tuple types.

It's been a while since I've done any C++. Can anyone clue me in?

Chris Rathman - Re: The New C++: Trip Report, October 2002  blueArrow
5/5/2003; 1:40:25 PM (reads: 2551, responses: 0)
Can't help much, as I think C++ is getting too complicated, but here's some links to the originals:

A Proposal to add a Polymorphic Function Object Wrapper to the Standard Library

Proposal for adding tuple types into the standard library

Paul Snively - Re: The New C++: Trip Report, October 2002  blueArrow
5/5/2003; 9:32:40 PM (reads: 2487, responses: 0)
I don't have anything to add to this other than the observation that as C++ continues to emphasize its "multiparadigm" nature, it becomes clearer that the other dominant paradigm it exposes, besides object-orientation, is a functional bent. I'd never thought of C++ as being in the same category of language as, say, Objective Caml, but I may have to reconsider.

Dan Shappir - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 12:59:40 AM (reads: 2154, responses: 16)
I think the links Chris provided more-or-less speak for themselves, but here are a few observations:

1. Both features originate from the Boost library. It seems this library is becoming a sort of testing ground for future C++ functionality. I wonder if this will continue, and perhaps expand to include Loki as well.

2. I'm currently finishing Andrei Alexandrescu's book "Modern C++ Design" which refers to Jaakko Järvi's tuple types explicitly (and explains their design) and also provides an implementation of polymorphic function object extended to support currying. Both require quit a bit of C++ hacking to implement.

3. Yes, C++ is getting a functional bent, and is that surprising? It's, after all, the path to the future ;-) Anyway, this isn't really a new thing. Consider STL algorithms like for_each and bind1st.

I'm not sure what sort of clues you are looking for Ehud, but the purpose of these additions appears to be pretty straightforward: a tuple type makes it easier to create functions that return multiple values (of various types) without jumping through the hoop of creating a specialized class or struct. I do wonder how many C++ programmers will use it instead of simply using out parameters, especially as sequence unpacking does not appear to be supported. Or put another way, how many C++ programmers will indeed go the functional way.

With regard to polymorphic function object, they appear to server two purposes:

1. Give C++ the semblance of functions as first-class citizens (thus allowing a bit of higher-order programming).

2. Unify the numerous ways C++ provides for expressing a function reference (function pointers, member function pointers, and arbitrary function objects).

It is interesting that both these features, neither of which were presumably in Stroustrup's sights when he conceived, designed and implemented C++, where accomplished as pure library extensions, requiring no changes to the core C++ language.

Dan Shappir - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 1:06:19 AM (reads: 2127, responses: 0)
An interesting general point to consider is the difference in attitude towards change as expressed by C++ and Perl. It seems that Perl's rate of change is greater by more than an order of magnitude than that of C++.

There may be various technical reasons for this but to me it seems that the reason is the differences in character, attitude towards PL design, and managing a developer community between Larry Wall and Bjarne Stroustrop.

Ehud Lamm - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 1:12:19 AM (reads: 2181, responses: 14)
Andrei Alexandrescu's book "Modern C++ Design"

I guess I should really get this book, right? You seem to think it's a good read.

Daniel Bonniot - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 2:30:27 AM (reads: 2156, responses: 0)
What is it that you call "sequence unpacking"?

In the proposal, I see: int i; char c; double d; tie(i, c, d) = make_tuple(1, a , 5.5);

Are you refering to something else?

Dan Shappir - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 2:32:11 AM (reads: 2216, responses: 2)
I guess I should really get this book, right? You seem to think it's a good read.

It is a good read, and I enjoyed it, but given this statement you also made:

It's been a while since I've done any C++.

I'm not sure I would recommend it to you. This book is very much hard-core C++ and may not be relevant for people working in other PLs. For example, there is a whole chapter detailing the intricacies of implementing a smart pointer class. Not much of an issue if you're working in Java, for example.

Another interesting point is that although this book is very hands-on (lots of code samples), there are very few compilers out there that can actually compile this code. In fact, there may only be one.

If you wish, when I finish the book and have a little spare time, I'll do a short review for LtU.

Dan Shappir - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 2:35:42 AM (reads: 2119, responses: 0)
What is it that you call "sequence unpacking"?

Yes, your sample is what I meant. I missed it skimming through the document. It's still much more verbose that what you would have done in Python:

i, c, d = make_tuple(1, a , 5.5);

Ehud Lamm - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 3:33:39 AM (reads: 2245, responses: 0)
I guess it would take me a couple of hours to get the dust of, but I think I'll manage...

Frank Atanassow - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 6:11:57 AM (reads: 2074, responses: 1)
Another interesting point is that although this book is very hands-on (lots of code samples), there are very few compilers out there that can actually compile this code. In fact, there may only be one.

This is really an amazing C++ phenomenon. Anyone who tries to exploit the language in serious ways needs a new compiler...

It's still much more verbose that what you would have done in Python:

In Haskell it's:

(i, c, d) = (1, a, 5.5)

In Ocaml you can even write:

i, c, d = 1, a, 5.5

Patrick Logan - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 6:32:17 AM (reads: 2071, responses: 0)
In Python, either...

i, c, d = (1, a , 5.5)

or...

i, c, d = 1, a, 5.5

There are some situations where the () are needed, but not always.

Andrei Formiga - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 7:08:27 AM (reads: 2056, responses: 0)
Well, I haven't tried it, but AFAIK gcc compiles Loki. Also, the only problem to compile Loki on Visual C++ was (past tense) the lack of support for partial template specialization.

The new version of VC++ (Visual C++ .NET 2003), claimed to be 98% compliant, does have support for partial template specialization, and should compile Loki, but then again, I haven't tried it.

Dan Shappir - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 7:26:04 AM (reads: 2107, responses: 0)
This is really an amazing C++ phenomenon. Anyone who tries to exploit the language in serious ways needs a new compiler...

It's the sad truth that the C++ Standard has been way ahead of mainstream C++ compilers for years. This also may be a reason why the C++ Standard has evolved so slowly in recent times.

Beside the bummer of having features dangled before your face yet being unable to use them, another downside it that some features appear in the Standard that have turned out to be bad ideas or have serious side-effects. This is the result of lack of in-the-field testing, e.g. the export keyword.

On the bright-side MS is claiming that their upcoming release of VC++ will be 98% compliant with the Standard. Since VC++ is the de-facto standard for C++ programming on Windows, this is indeed very good news.

BTW, one reason for this is that there has not been such a significant pressure on compiler developers from the C++ community to become more Standard compliant. Yes, there are the vocal participants on comp.lang.c++.moderated, but for the most part, the C++ community (if it can even be called a community) has been silent. From my experience many C++ developers are simply unfamiliar with the more advanced (or at least newer) features, and are reluctant to use them.

Another reason is that building a fully compliant C++ compiler is very difficult. From the second part of the export article:

The export feature alone took more than three person-years to code and test (not including design); by comparison, implementing the entire Java language took the same three people only two person-years.

Matt Hellige - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 7:36:53 AM (reads: 2148, responses: 10)
I guess I should really get this book, right? You seem to think it's a good read.

My response to this book (and I imagine many people around here might agree) was very much love/hate. The hacker in me loved the diabolical trickiness and the tireless drive to extract unplanned functionality from C++, but the engineer (and PL enthusiast) in me hated the diabolical trickiness and impenetrable idiom...

For example, I recall one case in the first couple of chapters where Alexandrescu writes a call to a function that is never defined, noting that this is OK because the "return value" is used only in a sizeof(), which is evaluated at compile-time, so really the compiler won't generate a call to said function (I may have gotten that slightly wrong, I don't have the book with me... that was the gist, though). More or less the whole book is full of stuff like that. At the end of the first few chapters, I wanted to giggle and gag at the same time, and my only coherent thought was, "Congratulations, C++ is the new Perl."

Certainly if I were forced to use C++, I'd be very glad for the tricks in the book (and in Loki, on a more day-to-day level), but honestly I'd much rather just choose a different language in the first place... I'd highly recommend the book, though, for entertainment value alone.

Ehud Lamm - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 7:44:39 AM (reads: 2187, responses: 0)
Thanks. I had a feeling this is what I can expect.

Dan Shappir - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 9:03:13 AM (reads: 2188, responses: 8)
I think that there is a significant difference, at least for C++, between the library programmer and the application developer, especially if it's a commercial library. Even within larger applications there is a difference between the core team and people building the functionality on top of that.

Consider a matrix library that allows users to manipulate matrices like regular values using operator overloading:

A = B + C;

There is no doubt that this makes it easier for the library's users. OTOH many C++ programmers don't know how to implement operator overloading, or fear it (indeed it was not included in Java because it was deemed "to complex"). So, C++ library developers need to understand operator overloading, but regular Joe developers may not.

The intricacies of templates are another example of this. Partial template specialization is incredibly important to the library developer. All the library user needs to know about is that he'll get the optimal implementation for his scenario.

One might claim that in an ideal PL there should be no difference between library developers and standard developers, that even powerful features should be easily accessible. While I definitely agree that many features could be made more accessible than they are in C++, I still believe that levels of expertise are inescapable in general purpose PLs.

Alexsanderscu wrote his book for the library developer (I believe this book is the result of his development of Loki). C++ is taken to its limit and then some. Some may see this as pure hackery, other as testament to the flexibility of C++.

And again, if the library writer uses hacks that make the library’s code more complex, but the end result is a library that is easier to use and more type-safe, is that a bad thing?

Ehud Lamm - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 9:10:49 AM (reads: 2216, responses: 5)
Of course, levels of expertise are inescapable, but I am not sure I understand why operator overloading should be considered a dark art. Can you enlighten me?

Some template trick are complex and require understanding intriacte details. Writing simple generic code, however, should be something any half decent programmer can do (even in C++).

Dan Shappir - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 9:24:35 AM (reads: 2248, responses: 4)
I am not sure I understand why operator overloading should be considered a dark art. Can you enlighten me?

I did not say operator overloading should be considered a dark art. All I said was that many C++ programmers don't know how to do operator overloading. And, if they do know about it, they consider it to be a complex feature that shouldn't be normally used. Still doesn't stop them from doing:

std::string a = "hello"; std::string b = " world"; std::string c = a + b;

And the sad fact is that many C++ programmers don't understand generics. Perhaps this is why Java still doesn't have generics. This is also why dynamic programming, where generics are a non-issue, is such a lure.

Ehud Lamm - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 9:36:39 AM (reads: 2282, responses: 3)
Hypothesis 1: "many C++ programmers don't know how to do operator overloading" because of the way C++ is designed (not only how the operator overloading mechanism works, but also other characteristics of the language).

Hypothesis 2: The same goes for generics.

It is these language design decisions I want to explore.

Alex Sauer-Budge - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 10:47:35 AM (reads: 2183, responses: 0)
Isn't every programmer a "library programmer"? In my experience, well factored application code often produces a collection of routines which can later be reused in other applications and thus naturally forms a little "library".

Matt Hellige - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 10:54:24 AM (reads: 2285, responses: 0)
It is these language design decisions I want to explore.

Exactly my point.

Dan: It's not that I think people shouldn't do these things in C++. As I said, if I were using C++, I'd certainly be thrilled to have those tools, both conceptually and more concretely in the form of libraries built by Someone Else. I just think the ugliness of the tricks you have to do to get certain kinds of basically macro-like functionality (for example) is kind of a sad testament to the mismatch between the design of C++ and how (at least some) programmers want to use it.

I don't disagree with you at all about the levels of expertise. Look at Lisp, for example. Within a given project, certainly not everyone needs to know how to write macros, redefine the parser's behavior, add load-time eval hooks and so on. But some programmers do want to do those things, and their lives sure are a lot nicer in Lisp (one could argue) than in C++... That's all.

Patrick Logan - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 11:13:19 AM (reads: 1999, responses: 1)
...many C++ programmers don't know how to implement operator overloading, or fear it (indeed it was not included in Java because it was deemed "to complex")

Java was designed to be "simple" not by driving simplicity to the core axioms, but by trimming pieces off here and there until the compiler writer had a handle on how to implement the language.

So while operator overloading is not per se a "dark art", to have included it in the original Java language would have blown wide open other holes in the complete definition and implementation.

Short cuts often aren't.

Ehud Lamm - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 11:56:28 AM (reads: 2035, responses: 0)
Can we try to be a bit more specific about operator overloading, for example?

Michael Vanier - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 1:45:01 PM (reads: 1979, responses: 1)
To do operator overloading correctly in C++ requires a deep understanding of the difference between references and non-references (especially in return values). If you're overloading the assignment operator (something you can't do in python, and IIRC, in C#), it's even more tricky because you should support the "a = b = c;" idiom. You have to know that "SomeObject o = SomeObject();" is a copy constructor call and not an overloaded assignment (the explicit keyword helps here). Many classes have user-defined conversions that make things even more complex. And you can also overload functions which are not methods of classes.

I believe firmly that C++ is a language for *experts* doing the most efficiency-critical kinds of applications, not for the average programmer doing average kinds of programs. This is why we have java and C#. If I don't need the fine-grained control that C++ offers for a particular project, I will use another language.

Ehud Lamm - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 2:10:26 PM (reads: 2010, responses: 0)
Now we are talking...

The problem is with reference vs. value semantics, and with handling assignments. These are two very specific language design issues. We mentioned both in the past.

The way C++ handles copy constructors (defualt and otherwise) is where the complexity begins.

Jeremy Leader - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 2:59:04 PM (reads: 1965, responses: 0)
One problem I see with the "library programmer" vs. "mere mortal" distinction is that C++ has such a steep gradient lurking between the two. If you want to overload an operator, you very quickly get into deep waters in C++, in order to prevent all sorts of bad things happening as a result of that one decision.

As a former C++ expert, I find the thought of trying to regain my former intimacy with the language daunting, because I don't seem to be able to re-learn small pieces as I need them. I'm either a god-like library programmer or I'm not. Contrast that with Perl, where I have been able to explore one grody dark corner of expertise at a time, while remaining blissfully ignorant of all the many others.

Perhaps C++ development placed too much emphasis on ensuring that unused features wouldn't hurt performance, and not enough on ensuring that unused features wouldn't hurt programmers.

Dan Shappir - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 3:09:37 PM (reads: 1980, responses: 1)
Can we try to be a bit more specific about operator overloading, for example?

This is actually a very good example - a seemingly simple, or at least straightforward, feature with not so obvious implications:

1. Did you know that the reference feature was added to C++ because of operator overloading? I read this in Stroustrup's "The Design and Evolution of C++". After all, given the matrix sample in my previous post, had the arguments to the + operator been passed by value, it would have been way to inefficient. And writing:

C = &A + &B;

would have been to weird.

2. Some operators, e.g. ->, can be overloaded while others, e.g. . , cannot. The reasons for this are often far from obvious.

3. Some operators can be overloaded but shouldn't:

  A. The addressoff operator & can be overloaded, but this play havoc with STL.

  B. The binary logical operators when overloaded behave differently than the intrinsic operators because of C/C++ short-circuit logic evaluation.

4. The far from obvious Koenig Lookup was added to C++ because of the weird interactions between operator overloading and namespaces.

5. As Michael pointed out there is a distinction between the assignment operator and copy constructor, although both can use the = symbol.

6. Also as Michael mentioned, what should the assignment operator return: void? a reference? and const reference?

7. If a class implements += should/must it also implement + ? What about vies-versa? And if it implements + should/must it implement - ? And what about ++ and -- ? And if you do need to implement them all, why can't the compiler deduce them automatically (yes, I know the answer).

8. How do you differentiate between the definition of prefix ++ and postfix ++ (answer - using a dummy int argument)

9. Consider the following class definition:

class X {
...
operator int();
int operator()();
...
};

What is the difference between the two (this is from my old C++ Riddles page) ?

10. Why can't some overloaded operators be static while others can?

11. Overloaded operators can result in some not-so-obvious ambiguities.

And yet, it's quit obvious that if a class appropriatly implements operator overloading, things are easier for its users (see my std::string example).

Isaac Gouy - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 3:37:38 PM (reads: 1958, responses: 0)
And it's reasonable to write software in C++ because... ?

Michael Vanier - Re: The New C++: Trip Report, October 2002  blueArrow
5/8/2003; 5:42:41 PM (reads: 1941, responses: 0)
Because you can write code in C++ that would be much less efficient if written in other languages of a comparable abstraction level.

Ehud Lamm - Re: The New C++: Trip Report, October 2002  blueArrow
5/9/2003; 12:52:25 AM (reads: 1976, responses: 0)
2. Some operators, e.g. ->, can be overloaded while others, e.g. . , cannot. The reasons for this are often far from obvious.

Are these all really operators? Perhaps, the language design should be more careful aobut what is considered an operator and what isn't, so this qurestion wouldn't arise?

3. Some operators can be overloaded but shouldn't

Ditto.

The binary logical operators when overloaded behave differently than the intrinsic operators because of C/C++ short-circuit logic evaluation.

Let's compare this to Ada. In Ada "and" and "or" are operators and can be overloaded, "and then" and "or else", the short circut versions, are not overloadable operators. This is clear from the language stadnard, and the reason behind the decision should be clear to most programmers.

5. As Michael pointed out there is a distinction between the assignment operator and copy constructor, although both can use the = symbol.

Should assignment really be considered an operator, or is it really a statement? The complications from treating assignment as an operators are many and often quite subtle.

Again, in Ada for example, changing the meaning of assignment isn't done be overloading the operator symbol directly.

6. Also as Michael mentioned, what should the assignment operator return: void? a reference? and const reference?

See above.

7. If a class implements += should/must it also implement + ? What about vies-versa? And if it implements + should/must it implement - ? And what about ++ and -- ? And if you do need to implement them all, why can't the compiler deduce them automatically (yes, I know the answer).

Are all these operators really necessary and worth the complexity they add to the language?

In Ada the most similar issue would be handling inequality (/=) when equality (=) is overloaded. The rule is that if the overloaded equality function returns a Boolean value, inequality cannot be manually overloaded, and is automatically set to mean "not equal."

8. How do you differentiate between the definition of prefix ++ and postfix ++ (answer - using a dummy int argument)

Are these operators worth the confusion they cause? Not just here, but also where order of evaluation is considered.

I chose some specific issues, and tried to show that different design choices are possible. Some of them make overloading much simpler, in fact, mundane. Obviously, there are tradeoffs, and other things may become more complicated as a result.

The point is, language design is about deciding about many such details, and we should be able to identify the possible approaches, and analyze them individually and collectively.

Ehud Lamm - Re: The New C++: Trip Report, October 2002  blueArrow
5/9/2003; 1:02:31 AM (reads: 2095, responses: 0)
Even if we agree about the distinction between library and application developers, I am not sure it should be mostly about programming language expertise.

Library designers should be more knowledgeable abour design techniques and have better design skills. Most of these skills should be easily trasferable between programming languages. At least languages based on the same paradigm and general approach (e.g., C++, Java, Ada, and C#).

Since this is LtU, I should perhaps be quick to add that there are several important techniques that are programming language dependent(e.g., Lisp macros). But these are more the excpetion than the rule.

Michael Vanier - Re: The New C++: Trip Report, October 2002  blueArrow
5/9/2003; 1:26:48 AM (reads: 1935, responses: 1)
Ehud, I think the question you're *really* asking (and one I ask myself all the time) is: would C++ be a better language if it was not so heavily wed to the conceptual model of C? I think the answer is a big, resounding "YES". Small details in C create big headaches in C++ because user-defined types have to behave like built-in types. In other words, C++ pays for C's mistakes.

For instance:

1. You have to support "chaining" assignment e.g. "a = b = c;". Therefore, when you overload assignment you MUST return a reference. If there were no chaining assignment then you could return void, and IMO the language would be superior. In other words, assignment should not return a value.

2. Supporting overloading of prefix and postfix ++ and -- is lame, as has been described. Frankly, I think having both forms of the operators is insanely stupid, since all it does is make code a tiny bit more concise, as well as less readable. I wish there were only one form (prefix is preferable, since then there would be no postfix operators) and I also wish ++ and -- didn't return values.

3. There shouldn't be any need for the "explicit" keyword. The only way to use a copy constructor would be by doing this:

Foo f(other_foo);

and not

Foo f = other_foo;

The latter would instead mean "create a new Foo using the default constructor and use the overloaded assignment operator to assign it the value of 'other_foo'". This is very inefficient, but it works fine with pointers:

Foo *f = some_function_returning_pointer_to_Foo();

The point is, there is no need for C++ to be quite as complex as it is (not to mention as it's going to be). Some of the complexity is justified, but much of it is just baggage. Maybe there's room for a new language that throws away the cruft and keeps the good stuff (and no, java and C# are not that language; they throw away too much).

Frank Atanassow - Re: The New C++: Trip Report, October 2002  blueArrow
5/9/2003; 6:22:04 AM (reads: 1903, responses: 0)
...and people say you need a PhD to do functional programming!

I would rather spend my time learning broadly applicable esoterica than C++-specific esoterica. (Though I suppose `broadly applicable esoterica' is something of a contradiction in terms.)

Chris Rathman - Re: The New C++: Trip Report, October 2002  blueArrow
5/9/2003; 6:26:24 AM (reads: 1919, responses: 0)
Maybe there's room for a new language that throws away the cruft and keeps the good stuff
The first thing you'd have to throw away is pointers/references and then add garbage collection - but then it wouldn't be C++ would it.

Frank Atanassow - Re: The New C++: Trip Report, October 2002  blueArrow
5/9/2003; 7:01:03 AM (reads: 1911, responses: 0)
The first thing you'd have to throw away is pointers/references and then add garbage collection - but then it wouldn't be C++ would it.

You can have garbage collection and explicit control over copying at the same time using some of the (experimental) languages founded on substructural logics like linear logic. Just because C++ lacks GC and doesn't know how to handle references and constants doesn't mean they are incompatible.

Isaac Gouy - Re: The New C++: Trip Report, October 2002  blueArrow
5/9/2003; 8:39:43 AM (reads: 1894, responses: 0)
...and people say you need a PhD to do functional programming!
Using Clean's uniqueness typing seemed challenging until I read this discussion... ;-)

Because you can write code in C++ that would be much less efficient if written in other languages of a comparable abstraction level.

Is that the strongest reason for using C++ ?

I'm lost in admiration for the cleverness of compiler writers.
This byzantine language is transformed into efficient code! Think what they could achieve if they started out with something less compromised.

Not being that clever, there's more chance that the software will be efficient if I use a less-complicated language that helps me identify hot-spots and inefficient algorithms.

Kimberley Burchett - Re: The New C++: Trip Report, October 2002  blueArrow
5/9/2003; 8:57:30 AM (reads: 1910, responses: 0)
Here's a couple more points about the subtleties of C++ operator overloading.

First, the -> operator is transitive! This is ostensibly in order to make it easier to create smart pointers. A smart pointer typically overrides its -> operator to return a pointer to the actual object. But the user usually wants -> to give them direct access to the object, not just a pointer to it. So C++ automatically calls operator -> as often as necessary, until it gets to a value that doesn't have a -> operator.

I'm not sure why references aren't a better solution to this problem. Maybe operator -> came before references?

Second, operators can be overloaded either within the class, or outside of it. For example, you can override the + operator within a class like this:

class Foo {
public:
    Foo operator +(const Foo &rhs);
}

Or you can override it outside of the class like this:

Foo operator +(const Foo &lhs, const Foo &rhs) {
   ...
}

The choice of whether to override in the class itself or not is almost completely a matter of style, until you decide that you want to be able to add objects of different types. For example:

Foo operator +(int i, const Foo &rhs) {
   ...
}

The above example must be declared outside the class, because the left-hand-side is an integer, and all binary operators declared inside a class take the class itself as the left-hand-side.

Sometimes you're forced to make the overloaded operator be outside of the class, but it needs to get access to the member variables of the class. In that case, you have two choices: you can declare the overloaded operator function to be a friend of the class, or if you're dealing with an associative binary operator, you can just swap the arguments (so that the class is on the left hand side), and delegate to the version which can be a member of the class.

There are even more subtleties that you have to consider when implicit conversions are thrown into the picture...

I think operator overloading in C++ is about halfway between language design and "normal" programming. This is what makes it scary to many programmers.

Paul Snively - Re: The New C++: Trip Report, October 2002  blueArrow
5/9/2003; 9:01:37 AM (reads: 1880, responses: 1)
There are a great many industrial settings in which the programmer must use C++.

So with that said, while there are definite subtleties to modern C++ when the majority of its features are used, I'm basically glad that the features are there. On the other hand, the comment about the gradient between "ordinary" and "library" C++ code being incredibly steep is well-taken: I'm glad it's Alexandrescu and company doing the latter rather than me.

On the third hand, I haven't found it to be true that only EDG-based C++ compilers can compile Loki: current GCCs do, as does Codewarrior Pro 8.3. More recently, I understand that Visual C++ 7.1 does as well.

If you have the luxury of choosing a better language to accomplish your goals, by all means do so: I certainly do under such ideal conditions. But when handed a C++ project, I'll leverage available libraries for all they're worth, and that definitely includes Boost and Loki.

Ehud Lamm - Re: The New C++: Trip Report, October 2002  blueArrow
5/9/2003; 1:27:41 PM (reads: 1842, responses: 0)
I think the important goal is not to make fun of C++, but rather to learn from its mistakes, and sub-optimal design decisions. The sames goes for other languages too. Personally, C++ bashing doesn't seem to me to be a worthy goal.

Ehud Lamm - Re: The New C++: Trip Report, October 2002  blueArrow
5/9/2003; 1:49:23 PM (reads: 1847, responses: 0)
would C++ be a better language if it was not so heavily wed to the conceptual model of C?

Sure, this is relevant for some of of issues we are discussing (e.g., do you really need i++ and ++i?). But not for all of them (e.g., binary methods and the class construct).

I think the problem is usually when you combine several otherwise attractive features, that don't play well with each other.

Dan Shappir - Re: The New C++: Trip Report, October 2002  blueArrow
5/9/2003; 2:26:36 PM (reads: 1834, responses: 0)
First, some defense of C++ (as if it needs me ;-)

would C++ be a better language if it was not so heavily wed to the conceptual model of C? I think the answer is a big, resounding "YES"

Stoustrup was once asked if he regrets his choice to build C++ on top of C. His answer, as I recall, was that if C wasn't around he would have picked whichever other language was most popular at the time and build on top of it. Stoustrup's intention was to bring OOP (as he experienced it in Simula) to the masses, thus he was willing to pay the price. That is C++ is defined by its being built on top of an existing PL.

Let's compare this to Ada. In Ada "and" and "or" are operators and can be overloaded, "and then" and "or else", the short circuit versions, are not overloadable operators.

Alas, the lack of this distinction was inherited from C, as well as even worse stuff such as the variable declaration syntax (which Stoustrup considered changing to something like Java's but didn't).

Are all these operators really necessary and worth the complexity they add to the language? (referring to += et al)

A resounding yes. A + B returns a new object while A += B modifies A in place (a concept which is certainly familiar to LISP programmers). As a result += implementation is very different from + (sometimes + is implemented using +=). C++'s emphasis on performance means the programmer must tailor the implementation of each operator specifically.

Are these operators worth the confusion they cause? Not just here, but also where order of evaluation is considered.

It's true that in C operators ++ and -- were simply syntactic sugar for +=1 and -=1 respectively. In C++ they turned out to be a lot more useful. This is because they implement a generic increment and decrement methods for both numeric basic types (including pointers) and user-defined types that implement them, even when such type don't (and shouldn't) implement += and -= (esp. with a numeric argument). An example of this is iterators. Many STL algorithms are dependent on ++.

Supporting overloading of prefix and postfix ++ and -- is lame, as has been described.

The method used to differentiate between the overloading of prefix and postfix ++ and -- is indeed lame, or at least a hack, but again the distinction itself is useful. True, you can get by without postfix ++ and -- (which returns the previous value of its operand) but the ability to express this functionality in such a concise maner is IMO useful.

There shouldn't be any need for the "explicit" keyword.

From my experience the purpose of the explicit keyword is mainly to prevent inadvertent implicit type conversions. C++ type conversions, and especially the implicit kind is another topic which deserves a review, but that is beside the point.

I would rather spend my time learning broadly applicable esoterica than C++-specific esoterica.

Most C++ programmers get by without ever learning the C++-specific esoterica. Indeed much of this esoterica is prohibited by several companies style guides. OTOH C++ library developers can leverage this esoterica to create better, more usable and more efficient libraries. And since they get paid for this, its worth their time and effort (and I'm guessing several of them enjoy it).

The first thing you'd have to throw away is pointers/references and then add garbage collection - but then it wouldn't be C++ would it.

Indeed it would not. There are several third party GC libraries for C++, and of course their is C++.NET with its managed objects. But the whole point of C++ is the total control that you loose when you use GC. Just imagine real time software that must complete a specific operation in a specific number of cycles and then the GC kicks in ...

Some additional observations:

The main issue with operator overloading is, of course, design - that is knowing when to use them (and when not). For example, would you use the += or << operators to add members to a collection (I generally wouldn't which is why I dislike the use C# makes of += to connect delegates to event generators). Most C++ programmers don't use operator overloading because they don't want to but also because they don't need to. The exception to this rule, as Ehud correctly pointed out, is the assignment operator. Though the same syntax as general operator overloading is used, it's indeed a different kind of beast: 1. The compiler automatically generates an assignment operator for a user-defined class if the developer doesn't explicitly specify one. 2. The assignment operator, like constructors, isn't inherited. 3. While overloading the other operators is strictly optional, as I explained above, you often must overload the assignment operator in order to guarantee proper resource ownership (The Law of the Big Three). Unfortunately, properly implementing the assignment operator in C++ can be tricky because of self assignment, const issues, exception safety etc.

Finally there is an issue of performance in operator overloading, consider:

D = A + B + C;

The straightforward implementation would translate to the following sequence of operations:

temp = A + B;
D = temp + C;

That is two object creations, one object destruction and four content copies. Using template metaprogramming techniques its possible to provide an implementation that doesn't require the temporary object and thus results in only one object creation, no destruction and three copies. In fact I've written an article about this once.

And now back to your regularly scheduled LtU programming.

Tim Sweeney - Re: The New C++: Trip Report, October 2002  blueArrow
5/9/2003; 4:15:01 PM (reads: 1803, responses: 1)
int f(int a,char b,bool c[10]);

struct {int a; char b; bool c[10];}

template<int a,char b,bool c[10]> class c;

tuple<int,char,bool[10]>

void g(void) {int a; char b; bool c[10]; ..};

Spot the five completely different sets of grammar and semantics to represent the notion of "an integer, a character, and 10 booleans" in different contexts.

andrew cooke - Re: The New C++: Trip Report, October 2002  blueArrow
5/9/2003; 4:20:17 PM (reads: 1929, responses: 0)
For example, there is a whole chapter detailing the intricacies of implementing a smart pointer class

is there a c++ book that doesn't have such a chapter?!

andrew cooke - Re: The New C++: Trip Report, October 2002  blueArrow
5/9/2003; 4:31:45 PM (reads: 2052, responses: 1)
Hypothesis 3: Neither are in C

Dan Shappir - Re: The New C++: Trip Report, October 2002  blueArrow
5/10/2003; 11:33:18 AM (reads: 1786, responses: 0)
Being able to do, or express, the same thing several different ways is necessarily a bad thing.

C++, being multi-paradigmatic and sporting C roots, unavoidably allows this.

Dan Shappir - Re: The New C++: Trip Report, October 2002  blueArrow
5/10/2003; 11:44:22 AM (reads: 2045, responses: 0)
I think few programmers these days come to C++ from C. This indeed means that C++'s once greatest asset has now become a liability.

Still people stick with C++, and there a numerous reasons for this: gets the job done (better the devil you know), legacy software, supported on most every platform, direct access to OS services, connect to most any other PL, good development environments, lots and lots of libraries, frameworks and sample code, industry standard (nobody ever got fired for programming in C++), efficiency (or the perception of it), multi-paradigmatic (only as much OOP as you want).