"return delete (new int)" compile but "return delete (new

X

X X

void fun1()
{
return delete (new int);
}

compiles, but

void fun2()
{
return delete (new std::string);
}

does not.

void fun3()
{
return (void) delete (new std::string);
}
compiles.

further, if I define a empty class

Class A {};
void fun4()
{
return delete (new A);
}
compiles

Does somebody knows
1) the reason why fun2() does not compile?
2) will the cast to void in fun3 introduce some side-effects/bugs ?

Thanks much.
 
A

Alf P. Steinbach /Usenet

* X X, on 17.07.2010 02:19:
void fun1()
{
return delete (new int);
}

compiles, but

void fun2()
{
return delete (new std::string);
}

does not.

void fun3()
{
return (void) delete (new std::string);
}
compiles.

further, if I define a empty class

Class A {};
void fun4()
{
return delete (new A);
}
compiles

Does somebody knows
1) the reason why fun2() does not compile?

It's an MSVC (Microsoft Visual C++) compiler bug. By the standard a 'delete'
expression returns 'void' no matter the argument. By the way you should have

1. Mentioned which compiler and compiler version.

2. Provided a complete program example.

3. Cited the error message.

See the FAQ item about how to post a question about Code That Does Not Work.

2) will the cast to void in fun3 introduce some side-effects/bugs ?

No.

But then the 'return' is entirely superfluous here.


Cheers & hth.,

- Alf
 
X

X X

Thanks Alf. You are right. The code is complied in VS 2010. The error
message is "c2562, void function returns a value".

The reason for return a void function is to write generic code. Think
about this:

template <typename T>
T dummyFun( T (*func)() )
{
//do some stuff
....

return func();
}

so the dummyFun works whether func() returns something or not.
 
Ö

Öö Tiib

Thanks Alf. You are right. The code is complied in VS 2010. The error
message is "c2562, void function returns a value".

Also most faqs of usenet strongly suggest against top-posting. Top-
posting makes it oot drah ot dnatsrednu.
The reason for return a void function is to write generic code. Think
about this:

template <typename T>
T dummyFun( T (*func)() )
{
//do some stuff
...

return func();

}

so the dummyFun works whether func() returns something or not.

How you provide delete keyword as template argument? If you know that
it is delete always then why you use its return value anywhere? delete
should return void.
 
R

red floyd

Also most faqs of usenet strongly suggest against top-posting. Top-
posting makes it oot drah ot dnatsrednu.




How you provide delete keyword as template argument? If you know that
it is delete always then why you use its return value anywhere? delete
should return void.

template<typename T> void deleter(T* p) { delete p; }

And pass deleter<T> to your function.
 

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

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top