Googles appar
Huvudmeny

Post a Comment On: cbloom rants

"08-12-11 - The standard cinit trick"

6 Comments -

1 – 6 of 6
Anonymous Anonymous said...

What is the anonymous namespace for?

I might actually steal this macro. It's less freakish than some of the other workarounds I've seen, presumably safer than doing nothing, and less annoying than calling all your init stuff from main (which is probably the only 100% reliable option :).

August 14, 2011 at 2:50 PM

Blogger cbloom said...

"What is the anonymous namespace for?"

So that definitions of the same class in different files don't conflict. It makes the decorated name unique to the current translation unit.

August 14, 2011 at 4:08 PM

Blogger won3d said...

Are linkers smart about laying out the code so that each pre-main init method doesn't cause a shiny new miss on startup?

I have some pretty mixed feelings on auto-registration. Now, I think it's not worth it. I guess, as a library author, manual init is a pain for your clients, who have better things to do than to become experts in your library.

Maybe a best-of-both-worlds is for the library to require manual init, and all the auto-registration actually happens in single optionally-static-linked file. That way, you can also deal with the static order initialization fiasco.

August 15, 2011 at 10:55 AM

Blogger cbloom said...

As a library author at RAD I'm very opposed to auto-init. In fact I'm trying to avoid CINIT at all because it imposes constraints on the client that they might not want.

The C++ cinit / singleton system works just fine, but it's one of those things in C++ that only works if you drink the whole kool-aid and use good meta-language rules, which not all people do.

In my personal code I love auto init.

August 15, 2011 at 11:31 AM

Anonymous Anonymous said...

I'm still not convinced that the anonymous namespace is necessary (I'm the Tom above btw), because the constructor, being implicitly inline, can legally have multiple identical definitions.

The only conflict-type thing I can spot here is the possibility of __LINE__ creating two structs with the same name in the preprocessed output - but anonymous namespaces won't save you from that, because they're per translation unit rather than per file.

My test in Xcode seemed to bear out my suspicion but perhaps there's some subtlety I'm missing and/or I need to go and read the C++ standard again.

(Also it took me about 5 minutes to re-find-out, again, the magic behind a working STRING_JOIN. So perhaps I shouldn't trust my C++ judgement in the first place.)

August 16, 2011 at 9:17 AM

Blogger cbloom said...

"I'm still not convinced that the anonymous namespace is necessary (I'm the Tom above btw), because the constructor, being implicitly inline, can legally have multiple identical definitions."

Yeah you may be right. Certainly in MSVC it seems to be legal to have identically named classes that do different things in different translation units.

BTW one niggle - the crucial thing is that they are *not* identical, the AT_STARTUPs in different files may do different things but have the same name. We don't want the linker collapsing them. Which it seems it doesn't.

In any case, I prefer to leave the anonymous namespace because it makes it clearer to me what's happening.

I strongly object to the school of thought that says you should remove syntax when it's unnecessary. I say you should add syntax when it makes it clearer what's happening. (eg. extra braces, extra parens, what have you).

August 18, 2011 at 3:49 PM

You can use some HTML tags, such as <b>, <i>, <a>

This blog does not allow anonymous comments.

Comment moderation has been enabled. All comments must be approved by the blog author.

You will be asked to sign in after submitting your comment.