c++-faq

What is the copy-and-swap idiom?

What is this idiom and when should it be used? Which problems does it solve? Will the idiom change when C++0x is used? Although it's been mentioned in many places, we didn't have any singular "what is it" question and answer, so here it is. Here is a partial list of places where it was previously mentioned: What are your favorite C++ ...

Understanding sizeof(char) in 32 bit C compilers

(sizeof) char always returns 1 in 32 bit GCC compiler. But since the basic block size in 32 bit compiler is 4, How does char occupy a single byte when the basic size is 4 bytes??? Considering the following : struct st { int a; char c; }; sizeof(st) returns as 8 as agreed with the default block size of 4 bytes (since 2 blocks are a...

C++ FAQ Lite Smart_Ptr Class Not Functioning?

I'm currently doing a lot of things on exception safety. (Herb Sutter's Exceptional C++, C++ FAQ Lite, etc) In particular, I wanted to write and understand the reference counting example of C++ FAQ Lite, but I'm currently stuck on this part of the code: class Smart_Ptr; class Foo { private: friend class Smart_Ptr; unsigned in...