Question
C++ delete syntax
I came across this rather unusual usage of 'delete'. Just wanted to know if the following line deletes both pointers or only the first?
delete ptr1, ptr2
Question
I came across this rather unusual usage of 'delete'. Just wanted to know if the following line deletes both pointers or only the first?
delete ptr1, ptr2
Solution
This is undoubtedly an error. The comma here is the comma operator, not a separator. Only the first pointer, ptr1
is deleted.
The second pointer, ptr2
, is just a do-nothing expression.
The delete
operator has higher precedence than the ,
operator, so the expression is parsed as if it were written:
(delete ptr1) , (ptr2)
and not as if it were written:
delete (ptr1 , ptr2)
If ,
had higher precedence than delete
, then only the second pointer would be deleted.
Solution
James McNellis is correct that this is comma operator, but he has the operator precedence wrong. He's (apparently) thinking it works out as:
delete (ptr1, ptr2);
in which case he'd be right -- it would only delete the second item. In reality, however, delete is also an operator, and has substantially higher precedence than the comma operator (which has about as low of precedence as possible), so it really works out as:
(delete ptr1), ptr2;
So it deletes the first pointer, and the result of the expression is the value of the second pointer -- which hasn't been deleted, so it's still valid (if it was previously).
I'd echo Martin York's conclusion, and I think this backs it up. I doubt that even one whole percent of C++ programmers know C++ as well as James McNellis -- when his answer about what a construct does is wrong, it's a solid indication that almost nobody will know what it really does. I'd posit that nobody at all can be sure it's doing what was intended. In fact, I'd guess it was intended to delete both objects, which it should not do (i.e., won't unless the compiler has a bug).
[Edit: I see that while I was writing this, James has corrected his answer. My apologies to James -- but I think the basic conclusion stands, so I'm not deleting this, at least for now.]