Why is sizeof(bool) not defined to be one by the C++ standard?

The other likely size for it is that of int, being the “efficient” integer type for the platform. On architectures where it makes any difference whether the implementation chooses 1 or sizeof(int) there could be a trade-off between size (but if you’re happy to waste 7 bits per bool, why shouldn’t you be happy to … Read more

Why does the implicit copy constructor calls the base class copy constructor and the defined copy constructor doesn’t?

That’s just the way the implicit copy constructor is defined (it wouldn’t make sense calling the default). As soon as you define any constructor (copy or otherwise) its normal automatic behavior is to call the default parent constructor, so it would be inconsistent to change that for one specific user-defined constructor.

What exactly is or was the purpose of C++ function-style casts?

Function style casts bring consistency to primitive and user defined types. This is very useful when defining templates. For example, take this very silly example: template<typename T, typename U> T silly_cast(U const &u) { return T(u); } My silly_cast function will work for primitive types, because it’s a function-style cast. It will also work for … Read more

Why do we need both using-directives and include-directives?

using directives and include preprocessor directives are two different things. The include roughly corresponds to the CLASSPATH environment variable of Java, or the -cp option of the java virtual machine. What it does is making the types known to the compiler. Just including <string> for example will make you able to refer to std::string : … Read more

Why can function templates not be partially specialized?

AFAIK that’s changed in C++0x. I guess it was just an oversight (considering that you can always get the partial specialization effect with more verbose code, by placing the function as a static member of a class). You might look up the relevant DR (Defect Report), if there is one. EDIT: checking this, I find … Read more

Is it allowed to use unions for type punning, and if not, why?

To re-iterate, type-punning through unions is perfectly fine in C (but not in C++). In contrast, using pointer casts to do so violates C99 strict aliasing and is problematic because different types may have different alignment requirements and you could raise a SIGBUS if you do it wrong. With unions, this is never a problem. … Read more

Why are default template arguments only allowed on class templates?

It makes sense to give default template arguments. For example you could create a sort function: template<typename Iterator, typename Comp = std::less< typename std::iterator_traits<Iterator>::value_type> > void sort(Iterator beg, Iterator end, Comp c = Comp()) { … } C++0x introduces them to C++. See this defect report by Bjarne Stroustrup: Default Template Arguments for Function Templates … Read more

Whatever happened to the ‘entry’ keyword?

I had no idea, so I googled to find something about this. This is what I found. First, it was included as a reserved keyword. Q: What was the entry keyword mentioned in K&R1? A: It was reserved to allow functions with multiple, differently-named entry points, but it has been withdrawn. (From http://archives.devshed.com/forums/c-c-134/c-programming-faqs-371017.html.) It was … Read more