0

Qt ソースを調べているときに、次のコードに遭遇しました。

template <typename T>
Q_INLINE_TEMPLATE void QList<T>::node_construct(Node *n, const T &t)
{
    if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new T(t);
    else if (QTypeInfo<T>::isComplex) new (n) T(t);
#if (defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__IBMCPP__)) && !defined(__OPTIMIZE__)
    // This violates pointer aliasing rules, but it is known to be safe (and silent)
    // in unoptimized GCC builds (-fno-strict-aliasing). The other compilers which
    // set the same define are assumed to be safe.
    else *reinterpret_cast<T*>(n) = t;
#else
    // This is always safe, but penaltizes unoptimized builds a lot.
    else ::memcpy(n, static_cast<const void *>(&t), sizeof(T));
#endif
}

それは奇妙なnew命令を持っています:

new (n) T(t);

私の知る限り、型キャストではないようです。この構造はどういう意味ですか?

4

1 に答える 1

7

これはplacement new。アドレスを指定してコンストラクターを呼び出すだけです。したがって、 type のオブジェクトがTlocation に構築されますn。また、コピー コンストラクターを呼び出すプレースメント new のようにも見えます。

于 2013-05-15T18:30:26.353 に答える