Why does comma expression not work as expected when used as the placement argument?
Asked Answered
A

1

10
#include <new>

using namespace std;

void f(void*)
{}

struct A
{};

int main()
{
    A a;

    f((a.~A(), &a)); // OK
    new (&a) A();    // OK

    new ((a.~A(), &a)) A(); // error C2059: syntax error : 'type'
}

I think (a.~A(), &a) is a valid expression which can be evaluated to a pointer value, so it should be accepted as the placement argument, why is the result not so?

My compiler is VC++ 2013 RC. Is this a compiler bug?

Update:

I have summitted a bug to connect.microsoft.com

Astragal answered 27/9, 2013 at 3:12 Comment(1)
Looks like a bug to me.Kr
P
3

Yes, it is a compiler bug, the syntax is correct.

You can look at the grammar in the standard:

new-placement:
( expression-list )

And, a.~A(), &a is valid as an expression-list.

Pierrette answered 27/9, 2013 at 3:23 Comment(3)
I think you misunderstood what's going on. The new is getting a single argument: two expressions separated by the comma operator and surrounded by parentheses. Not accepting it is still a compiler bug, though. (I'm not sure on the validity of taking the address of a dead object.)Macy
@CTMacUser: taking the address of a dead or not yet constructed object is valid, though the operations you can do with such an address are very limited since any attempt to dereference it is undefined behavior.Plumy
@CTMacUser: Yes, that is how the comma operator works, but its failing as a syntax error, so its not evaluating the operands at all.Pierrette

© 2022 - 2024 — McMap. All rights reserved.