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

Discussion in 'C++' started by X X, Jul 17, 2010.

  1. X X

    X X Guest

    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.
     
    X X, Jul 17, 2010
    #1
    1. Advertising

  2. Re: "return delete (new int)" compile but "return delete (new std::string)"not, why?

    * 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

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Jul 17, 2010
    #2
    1. Advertising

  3. X X

    X X Guest

    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.



    On Jul 16, 7:34 pm, "Alf P. Steinbach /Usenet" <alf.p.steinbach
    > wrote:
    > * 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
    >
    > --
    > blog at <url:http://alfps.wordpress.com>
     
    X X, Jul 17, 2010
    #3
  4. X X

    Öö Tiib Guest

    On Jul 17, 4:02 am, X X <> wrote:
    > 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.
     
    Öö Tiib, Jul 18, 2010
    #4
  5. X X

    red floyd Guest

    On Jul 18, 7:35 am, Öö Tiib <> wrote:
    > On Jul 17, 4:02 am, X X <> wrote:
    >
    > > 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.


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

    And pass deleter<T> to your function.
     
    red floyd, Jul 19, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HeroOfSpielburg
    Replies:
    1
    Views:
    414
    Alf P. Steinbach
    Aug 6, 2003
  2. Jef Driesen
    Replies:
    1
    Views:
    529
    Gernot Frisch
    Jan 19, 2005
  3. Nagaraj
    Replies:
    1
    Views:
    920
    Lionel B
    Mar 1, 2007
  4. fAnSKyer
    Replies:
    2
    Views:
    568
    Alf P. Steinbach
    Jun 7, 2009
  5. Steve Anderson
    Replies:
    3
    Views:
    282
    Steve Anderson
    Jun 21, 2004
Loading...

Share This Page