why is "delete" an expression?

  • Thread starter Stefan Strasser
  • Start date
S

Stefan Strasser

why is delete an expression and not a statement? (in my draft copy of
the standard).
I was about to ask the same question about "throw" but found an
expression case of throw("return boolvalue ? 5 : throw 5;").
but "delete" neither does exit the scope nor has a return value.

any idea?


thanks,
 
R

Ron Natalie

Stefan said:
why is delete an expression and not a statement? (in my draft copy of
the standard).
I was about to ask the same question about "throw" but found an
expression case of throw("return boolvalue ? 5 : throw 5;").
but "delete" neither does exit the scope nor has a return value.

any idea?

Most likely to make it symmetrical with new, which is also an
expression. Yeah I know it's lame. But your conditional operator
is just as valid for delete as throw.

boolvalue ? delete something : other_void_exression();
 
V

Victor Bazarov

Stefan said:
why is delete an expression and not a statement? (in my draft copy of
the standard).
I was about to ask the same question about "throw" but found an
expression case of throw("return boolvalue ? 5 : throw 5;").
but "delete" neither does exit the scope nor has a return value.

It does have a return value. It's of type 'void'. And what does
exiting the scope have to do with it? A call to a function that
returns 'void' is an expression, for example.

You can put an expression as the third part of 'for':

for (int i = 0; i < 10; delete a[i++])
cout << a;
delete[] a;

but you wouldn't be able to put a statement there.

V
 
S

Stefan Strasser

Ron said:
Most likely to make it symmetrical with new, which is also an
expression. Yeah I know it's lame. But your conditional operator
is just as valid for delete as throw.

boolvalue ? delete something : other_void_exression();

oops, you're right, that is a reason.
I only tried it with a non-void type and was mislead by a gcc error:

test.cpp:6: error: `operator delete(blah)' has type `void' and is not a
throw-expression


no mention of delete, and no example with delete in std draft(it has one
about throw).

thanks!
 
O

Old Wolf

Stefan said:
why is delete an expression and not a statement? (in my draft copy of
the standard).

All expressions can be turned into a statement by
appending a semicolon. (There are also other types of
statement).
I was about to ask the same question about "throw" but found an
expression case of throw("return boolvalue ? 5 : throw 5;").
but "delete" neither does exit the scope nor has a return value.

'delete' is actually an operator. It is a unary operator (takes
one argument) and evaluates to a type of 'void'.

Cf. the unary operator 'sizeof' which takes one argument and evaulates
to a type of 'size_t'. Also cf. the unary operator '-' which
evaluates to the type of its argument. Don't be confused by the
fact that some operators have alphabetical names, and some don'.

Any use of an operator with valid operands, is an expression.

I think it is simpler for the C++ grammar to add 'delete' in
along with the other unary operators, rather than to define
a new type of statement especially for 'delete'.

Interestingly, we can have this valid but bizarre code:

void foo()
{
char *p = boo();
if (bar())
return delete p;

baz(p);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,289
Messages
2,571,435
Members
48,121
Latest member
ColinHibne

Latest Threads

Top