Flutterby™! : C++ gotcha

Next unread comment / Catchup all unread comments User Account Info | Logout | XML/Pilot/etc versions | Long version (with comments) | Weblog archives | Site Map | | Browse Topics

C++ gotcha

2005-09-21 18:07:50.609649+00 by Dan Lyke 7 comments

C++ gotcha of the moment: In one file, have a global that looks something like: vector alongX(1,0,0);, in another file, have a static or global that is initialized as vector up=alongX;. Problem: there's no guarantee of constructor ordering.

[ related topics: Software Engineering ]

comments in ascending chronological order (reverse):

#Comment Re: made: 2005-09-21 18:58:27.967866+00 by: aiworks

My experience is that global constructor handling ends up being very compiler specific. Sometimes, statics get instantiated before globals. I've seen some compilers where sometimes global constructors don't get called at all.

Can you change it to vector &up=alongX? There should be no problem with the reference getting set before the constructor fires.

#Comment Re: made: 2005-09-21 19:54:16.624167+00 by: Dan Lyke

In this case it's actually compiler flags setting specific, of the 5 or 6 standard build modes, it happens in one or two of them.

I think that the better solution might be either using a const or a #define for the original. Can't use the reference because we want to change the latter, not just alias it.

#Comment Re: made: 2005-09-21 20:58:30.979472+00 by: mvandewettering

Welcome to C++, where the language semantics are sufficiently ill-defined as to be completely annoying. Rather than fix the language, ideas like:

http://www.doc.ic.ac.uk/lab/cplus/c++.rules/chap7.html#Example29

seem to be recommended. Bleh.

#Comment Re: made: 2005-09-21 22:16:40.192187+00 by: Dan Lyke

Yeah, I'd done a little bit of C++ 2 a while ago, but in getting back into it I've been shocked and amazed by the level of kludge around kludge and general cognitive load that the language requires. Unfortunately, nobody's come up with a solution that works better other than "build everything in C as an extension to an interpreted language", and there are some applications for which that isn't quite feasible.

#Comment Re: made: 2005-09-21 22:40:10.081841+00 by: Dan Lyke

Snort! The Objectivists[Wiki] will love this one: that document you linked has an explanation of why a = a has undefined behavior in C++...

#Comment Re: made: 2005-09-22 02:43:01.806311+00 by: spc476

Now I don't feel so bad about skipping C++. I'm also amazed that they actually have to cover the a = a case—shouldn't that be optimized out by the compiler anyway? Or do people do that just for the side effects?

My brain hurts.

#Comment Re: made: 2005-09-22 16:18:52.404472+00 by: Dan Lyke

Yeah, it's that everything has a freakin' side effect. This is why the C++ weenies are screaming ++a rather than a++, because the potential side effects are that the compiler may make a copy of the object, and with a heavyweight object that might be an expensive operation, and...

In fact, I'm not sure if the spec says that the compiler has to make a copy of the operation, but you could imagine that because of the side effects it might have to.