[LLVMbugs] [Bug 10197] New: Instantiation of a class (from a template parameter) with "new" gives incorrect object initialization

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sun Jun 26 12:40:43 PDT 2011


http://llvm.org/bugs/show_bug.cgi?id=10197

           Summary: Instantiation of a class (from a template parameter)
                    with "new" gives incorrect object initialization
           Product: clang
           Version: 2.8
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
        AssignedTo: unassignedclangbugs at nondot.org
        ReportedBy: adrianosf at gmail.com
                CC: llvmbugs at cs.uiuc.edu, dgregor at apple.com


The code below produces strang result (and different than GCC). It seems clang
lost yourself when using the new operator with a template parameter.

The C++ standard says:
---------------------
— If the new-initializer is omitted:
— If T is a (possibly cv-qualified) non-POD class type (or array
thereof), the object is default-
initialized (8.5). If T is a const-qualified type, the underlying class
type shall have a user-declared
default constructor.

and

To default-initialize an object of type T means:
— if T is a non-POD class type (9), the default constructor for T is
called (and the initialization is ill-
formed if T has no accessible default constructor);
---------------------

But I see in the disassembled code that clang explicitely assigns a 0 to the
member variable.

GCC result: 102, 103
clang result: 0, 103


//----------------------------------------
#include <stdio.h>


class C1
{
public:
    C1()
    {
    }

    void* operator new(size_t size, int n)
    {
        C1* p = (C1*) ::new char[size];
        p->n = n;
        return p;
    }

    int n;
};

template <typename T>
class Factory
{
public:
    T* newItem(int n)
    {
        return new(n) T;
    }
};


class C2 : public C1
{
public:
};

Factory<C2> factory;


int main()
{
    C2* a = factory.newItem(102);
    C2* b = new(103) C2;

    printf("--> %d, %d\n", a->n, b->n);

    return 0;
}
//----------------------------------------

-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list