空クラスのnew
http://b.hatena.ne.jp/entry/http://d.hatena.ne.jp/Isoparametric/20080124/1201185062
のid:sankasekiさんのコメントに関して補足。
「空クラスをnewしたらNULLになるんじゃないの?」という意見ですが、規格では、
5.3.4/10
ISO/IEC 14882/2003
A new-expression passes the amount of space requested to the allocation function as the first argument of type std::size_t. That argument shall be no less than the size of the object being created; it may be greater than the size of the object being created only if the object is an array.
となっていて、例として
5.3.4/12
[Example:
- new T results in a call of operator new(sizeof(T)),
とも書いてあることから、operator new(std::size_t)が呼ばれることが分かります。
さらに、sizeofの説明
5.3.3/2
When applied to a class, the result is the number of bytes in an object of that class including any padding required for placing objects of that type in an array. The size of a most derived class shall be greater than zero (1.8).
からsizeof(T)は1以上となるので、operator new()へ渡される引数も1以上となります。
そもそも、operator new(std::size_t)の仕様が
18.4.1.1/3
Required behavior: Return a non-null pointer to suitably aligned storage (3.7.3), or else throw a bad_alloc exception. This requirement is binding on a replacement version of this function.
となっており、例えサイズが0であったとしても、NULLが返ることは決してありません。
というわけで、空クラスをnewしてNULLが返るのは、コンパイラのバグか独自の拡張なのでご注意ください。
まぁ、今回の例ではvtblがあるので空クラスでもないんですけどね。