In this C++ Challenge, Herb Sutter throws down the gauntlet. Can you get around the C++ rule of using constructed objects as members of unions? Find out the answer in this sample chapter.
In this C++ Challenge, Herb Sutter throws down the gauntlet. Can you get around the C++ rule of using constructed objects as members of unions? Find out the answer in this sample chapter.
Eugenia – I love when you post these! Thanks!
“How to push a C++ compiler into generating internal errors in 6 easy steps!”
Seriously? How does this help me make more money from commercial software?
How does this help me make more money from commercial software?
Not a bit I guess. That doesn’t make the article worthless, though.
> What problem does this article address?
That of people not understanding the more obscure corner-cases of the language.
> How does this help me make more money from
> commercial software?
By making less mistakes.
Erm… wasn’t that obvious? Or did I misunderstand your question?
I thought this website was all about Operating Systems. Why are we seeing so many article about programming? If I was looking for programming info, I’d visit a programming site. OSNews is losing its focus…….
This kind of trickery is the reason why C++ sucks. It totally breaks the idea of high-level programming, when one does unions of objects.
where “sucks” is defined:
provides a generally good ride, yet supports popping the hood for low-level tweaking that I do not understand, therefore I revile, regardless of how crucial such capacity might be on occasion.
– Maybe it’s time we started labeling C coding ideas with a “Stupid” label, or java’s “Depricated” label if we want to be nice.
– From how many angles are Unions a bad idea, except on an Atari 800 for 64k of memory.
– I don’t know if the Operating System programmers can still justify using Unions in the OS for space/performance, but, in business software, they should Never be used.
Read the article, even the author gives you good reasons not to us these tricks. But, I can think of one: Security.
When is it a good idea to overlay a Double memory space with String Data! Only a fool would use unions today.
C++ is way too complicated for what it does. C++ programmers seem to enjoy memorizing the huge, intractable language spec with all its arcana, luckily the rest of us have real work to do in well-designed languages…
<language I don’t understand> is way too complicated for what it does. <language I don’t understand> programmers seem to enjoy memorizing the huge, intractable language spec with all its arcana, luckily the rest of us have real work to do in <language I understand>…
I do understand C++, thank you very much. Just beacuse somebody understands C++ doesn’t mean they will like it.
…and what are these “well-designed” languages?
provides a generally good ride, yet supports popping the hood for low-level tweaking that I do not understand, therefore I revile, regardless of how crucial such capacity might be on occasion.
I like C++ to the extent that it’s pretty decent for a mainstream language. I use it a lot, partly because I have to for work. But it was stuff like this that really pushed me to better languages for personal use. Templates and the STL really open up a whole new world of techniques, and when you first learn them, you’re all excited about the possibilities.
Then you realize that you’re fighting the language and compiler the whole way, using tricks to implement features that could have been there in the first place. The breaking point for me came when I realized that no matter how much hackery the Boost libraries used, proper closures or lambdas were just not happening in C++, not in practice anyway. Hint: when the subject of an article by a C++ luminary involves getting around a basic language rule, something is amiss!
C, Java (its not great but better than C++), Lisp, Smalltalk, Haskell, Slate, Prolog, tons of others…
(language I don’t understand) is way too complicated for what it does.
C++ isn’t hard to understand. The spec is arcane, but he semantics aren’t anything that will really make you think. Dislike of C++ rarely stems from a lack of understanding. Indeed, it was when I really started to understand it that I became less enthusiastic about it. Don’t get me wrong, I think it’s a generally decent language for its point in the design space, it just doesn’t warrent the sort of noble defense that you’re making for it.
The article describes (I’ve only browsed quickly) unions as some sort of polymorphism / variant, and encourages to reuse one variable for different types. This is not what unions are for.
Unions are for accessing the same data in other ways, for example an ARGB union
union ARGB {
uint32 argb;
byte components[4];
};
This way you can access the argb components in an easy way, while you still only have one 32bit.
Unions are/were also used for cpu registers (REGS)
Enjoyed both of your responses.
Possibly coming at your point from another angle, I used to think C++ was the ultimate, for the usual macho reasons, until trying out Python, and then getting into Lisp.
The irony of C++ is that the “cutting edge” stuff in the C++ In Depth series from Addison-Wesley is deeply concerned with, for example, lazy evaluation, from the functional community. Why? It’s the only way to disassemble the atomic bomb that was brought on by templates.
Nah, I wasn’t mounting a noble defense so much as fishing for arguments not of the form “*my* favorite is cool and all else is weak”.
“Can you get around the C++ rule of using constructed objects as members of unions?”
Yes, by using a decent programming language instead. Next question!
(And yes, this is a bit of a humorous dig, having learned C++ back when some OSNews contributors were probably still in short pants, but having used it pretty recently, too.)
well perhaps unions don’t quite work on c++, so what? this is just like multiple inheritance, it just doesn’t work quite well on some languages but seems to work great on others like eiffel. Eiffel programmers probably laugh at half baked solutions like java/c# interfaces.
The article describes (I’ve only browsed quickly) unions as some sort of polymorphism / variant, and encourages to reuse one variable for different types. This is not what unions are for.
Unions are for accessing the same data in other ways, for example an ARGB union
No. While the re-interpretation is certainly an application of unions, it is not the only one – nor even the main one. VM interpreters are one example where unions are used to save memory, not reinterpret value bit patterns.
<cite>C++ is way too complicated for what it does</cite>
If you don’t want learn/know advanced C++ concepts then stay away from them –or just use libs from Boost repository.
No problem! C++ is for dummies too.
the argument that we aren’t expert enough to program effectively in C++ is the very reason for concluding that C++ is a terrible development.
for the same reason that hordes of programmers (not the always same as computer scientists) excuse perl with a “buts its regular expressions are great”.
other languages approach regex in a much more elegant manner.
other languages are much more appropriate than C++.
i challenge anyone here to provide me with an example when C++ is the *best* solution to a problem and its constraints and its environment.
> i challenge anyone here to provide me with an example
> when C++ is the *best* solution to a problem and its
> constraints and its environment.
When you can get your hands on a team of people skilled in C++ easier than a team of people skilled in some superior but less common language.
Much of the “ugliness” of C++ comes from staying mostly source compatible with C. That’s also why it was so successful.
We’re not living in a limbo. You might prefer Python anytime, but knowing C++ and Perl gives you a better chance at getting a job than knowing Python and Lisp.
That’s why people continue to learn C++, and why it’s a language that’s here to stay a while longer.