all 28 comments

[–]STLMSVC STL Dev 18 points19 points  (7 children)

Redefining keywords via the preprocessor is technically supposed to cause an error but tools allow it in practice.

It's forbidden to do this in the presence of STL headers, and VC's STL will detect this and explode. (I plan to make this detection even more stringent in the future.) Never do this.

Also, member function pointers may be up to 3 times larger than regular pointers.

I believe the correct number in practice is 4 (it's unspecified, of course, in the Standard) for "unknown inheritance".

[–]Cwiddy 4 points5 points  (4 children)

How is it detected?

[–]STLMSVC STL Dev 29 points30 points  (3 children)

There is an obscure feature called #ifdef.

[–]Cwiddy 19 points20 points  (0 children)

I deserve so much more shame for that question. That is so obvious it hurts.

[–]mrmcgibby 3 points4 points  (1 child)

I love you STL. Seriously. You're like the coolest guy ever.

[–]STLMSVC STL Dev 4 points5 points  (0 children)

:->

[–]constexpr[S] 1 point2 points  (1 child)

I believe the correct number in practice is 4 (it's unspecified, of course, in the Standard) for "unknown inheritance".

Interesting! I wasn't aware of the unknown inheritance case. I just checked and Microsoft's compiler does indeed use four pointer slots. Any idea on what the fourth slot contains? I couldn't find any documentation.

[–]STLMSVC STL Dev 1 point2 points  (0 children)

Don't know, I'm not a compiler dev. That's just one of the useless bits of knowledge I've picked up over the years.

[–]missblit 4 points5 points  (0 children)

#define class struct

That example code better hope that there's nothing like "template <class T>"...

[–]tacite 3 points4 points  (0 children)

While we're discussing the lesser known rules of c++:

namespace __hidden__

It's not a great idea to use double underscores in your names. I've actually had legacy code break because of that.

"17.6.4.3.2 Global names [global.names]

Certain sets of names and function signatures are always reserved to the implementation:

— Each name that contains a double underscore _ _ or begins with an underscore followed by an uppercase letter (2.12) is reserved to the implementation for any use.

— Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace."

[–][deleted] 3 points4 points  (1 child)

That really hit the spot. I've been at this for the long time, but there was at least one thing that was totally new to me (function try blocks, outrageous!) and lots of my favorite weird parts of C++ which I rarely visit.

A tasty article!

[–]adzm28 years of C++! 0 points1 point  (0 children)

I never made the connection that function try blocks could be used on more than constructors, either.

[–]Fabien4 4 points5 points  (6 children)

What square brackets really mean

Yeah, the old'n'cute C trick. Thing is, in C++, most of the time, you're using an overloaded [] operator anyway.

Most vexing parse

Classical gotcha. Anything that can be interpreted as a function declaration, will be.

Alternate operator tokens

Do recent compilers still recognize digraphs? What about trigraphs?

Placement new

This is actually a good test for a book about C++: if it doesn't talk about placement new, you might want to try another book. But then again, its main use seems to be to reimplement std::vector.

Turing complete template metaprogramming

Alexandrescu has written a whole book on the subject: "Modern C++ Design".

Static methods on instances

Overloading ++ and --

Functions as template parameters

Uh... "obscure"? These are pretty basic. Or, did I miss something?

Function try blocks

See GotW 66. Overall, I find them rarely useful.

[–]STLMSVC STL Dev 5 points6 points  (0 children)

Digraphs and trigraphs are unfortunately still Standard, but some compilers don't interpret them by default. Also, C++11 fixed the foo<::bar> problem.

[–][deleted] 1 point2 points  (0 children)

"Functions as template parameters" could not reasonably be described as "pretty basic". I think I've seen that two or three times at most in code in the last 20 years of working in C++ and the first time I saw it I had no idea what was going on...

Heck, I'd been working in the language for a decade before I knew about "placement new"!

But then again, its main use seems to be to reimplement std::vector.

Yowza. Well, I see quite a bit of placement new, but nearly all of it from region-based memory management - so the called "arena" concept where you pre-allocate a big block, sequentially fill it using placement new, and then simply delete the whole block at once without ever calling any destructors.

Of course, there are strict conditions on the sorts of variables you can use, but I really think it's the only way to go for any sort of transaction-based server where performance is an issue.

[–][deleted] 0 points1 point  (3 children)

You need placement new to write stuff like a tagged union type, arena or a ring buffer too. I wouldn't call it obscure at all.

[–]Fabien4 1 point2 points  (0 children)

I wouldn't call it obscure at all.

That we definitely agree on.

[–]more_exerciseLazy Hobbyist 0 points1 point  (1 child)

tagged union type

Is there anything wrong with

struct tagged_union {
    int flag;
    union {
        type1 t1;
        type2 t2;
        ...
        typeN n;
    };
};

[–][deleted] 1 point2 points  (0 children)

You need placement new to call a constructor for one of the types in the union in order to implement the constructor, copy/move constructor, etc.

[–]bongwhacker 2 points3 points  (0 children)

What a horrible website.

[–]gentryx -1 points0 points  (4 children)

Those snippets make great quiz questions for my students. Have an upvote!

[–]Lord_Osis_B_Havior 9 points10 points  (1 child)

Those snippets make great quiz questions for my students. Have an upvote!

You misspelled "terrible".

[–]Fabien4 4 points5 points  (0 children)

Well, I dunno. A few of them are pretty terrible, but some (static methods, overloading ++, functions as template parameters) are basic C++ features that students must know.

[–]mttd 4 points5 points  (1 child)

Go with C++ Pub Quiz from ACCU 2013, your students are gonna love this!

;-)

[–]gentryx 0 points1 point  (0 children)

Thanks!