Strange casting problem w/ GCC 3.3

Discussion in 'C++' started by destroyedlolo, Jul 2, 2003.

  1. Hi all,

    I just upgrade my workstation to Gcc 3.3, and I got a very strange
    probleme with casting : I got an error if I try to get the adress of a
    "casted" pointer.


    ---- dumy example source code ------

    class tata {
    int a;
    };

    int main(int, char **){
    void *toto;
    tata **tst_ptr = &((tata *)toto);
    }

    -------------------------------

    [faillie]/home/faillie/src $ g++ tst.cxx
    tst.cxx: In function `int main(int, char**)':
    tst.cxx:7: error: non-lvalue in unary `&'

    I duno if it's normal or not (I don't see why the compiler think this
    casted stuff is not a "lvalue"), but do you know how to compile such
    thing ?

    If it's a GCC bug, I'm very bad as it's the only version I have on my
    HPUX workstation :-(

    Thanks for your help

    Laurent
    destroyedlolo, Jul 2, 2003
    #1
    1. Advertising

  2. (Shane Beasley) wrote in message news:<>...

    > > Write the result of the conversion into an own variable and take a
    > > pointer to that, and try to avoid old-style casts:
    > >
    > > void *toto;
    > > tata *converted_toto = reinterpret_cast<tata *>(toto);
    > > tata **tst_ptr = &converted_toto;

    >
    > Alternatively, you could use a reference, which is an lvalue and thus
    > has an address you can take:
    >
    > void *toto;
    > tata **tst_ptr = &reinterpret_cast<tata *&>(toto);


    Hmm... My code was meant to do what the OP asked to do, which is not
    the same as what the code above does. Oops. :)

    However, since conversion from tata * to void * is implicit, the
    reverse can (and should) be done with static_cast, not
    reinterpret_cast:

    tata *converted_toto = static_cast<tata *>(toto);

    I would agree that this is the sanest route, all things considered.

    - Shane
    Shane Beasley, Jul 3, 2003
    #2
    1. Advertising

  3. Rolf Magnus <> wrote in message news:<bdugsj$t6c$00$-online.com>...

    Hello,

    > casting toto into a tata* creates a nameless temporary (the "casted
    > stuff") that contains the converted value. And this temporary is not an
    > lvalue. You cannot take the address of it.


    For me it's a "surprise" for me, because doing such cast, I said to
    the compiler "ok, even if toto is a void *, please treat it as a tata
    *" ... as it done w/ normal C or w/ older version of the compiler. If
    I want realy a modification of my object, I said dynamic_cast<> or
    something like that.

    Thanks for your light and code.

    Bye

    Laurent
    destroyedlolo, Jul 3, 2003
    #3
  4. Rolf Magnus <> wrote in message news:<bdugsj$t6c$00$-online.com>...
    > destroyedlolo wrote:
    >
    > > Hi all,
    > >
    > > I just upgrade my workstation to Gcc 3.3, and I got a very strange
    > > probleme with casting : I got an error if I try to get the adress of a
    > > "casted" pointer.
    > >
    > > ---- dumy example source code ------
    > >
    > > class tata {
    > > int a;
    > > };
    > >
    > > int main(int, char **){
    > > void *toto;
    > > tata **tst_ptr = &((tata *)toto);
    > > }
    > >
    > > -------------------------------


    > casting toto into a tata* creates a nameless temporary (the "casted
    > stuff") that contains the converted value. And this temporary is not an
    > lvalue. You cannot take the address of it.
    >
    > > but do you know how to compile such thing?

    >
    > Write the result of the conversion into an own variable and take a
    > pointer to that, and try to avoid old-style casts:
    >
    > void *toto;
    > tata *converted_toto = reinterpret_cast<tata *>(toto);
    > tata **tst_ptr = &converted_toto;


    That compiles but won't work as expected. You'd want
    (*tst_ptr) = new tata;
    to set the void* to the new tata. After all, tst_ptr should
    point to toto.

    Regards,
    --
    Michiel Salters
    Michiel Salters, Jul 3, 2003
    #4
  5. destroyedlolo

    Rolf Magnus Guest

    destroyedlolo wrote:

    > Rolf Magnus <> wrote in message
    > news:<bdugsj$t6c$00$-online.com>...
    >
    > Hello,
    >
    >> casting toto into a tata* creates a nameless temporary (the "casted
    >> stuff") that contains the converted value. And this temporary is not
    >> an lvalue. You cannot take the address of it.

    >
    > For me it's a "surprise" for me, because doing such cast, I said to
    > the compiler "ok, even if toto is a void *, please treat it as a tata
    > *"


    That's not what a cast is doing. A cast converts the specified value to
    the destination type and returns the result of that conversion.

    > ... as it done w/ normal C or w/ older version of the compiler.


    Which compiler? Btw, "normal C" behaves the same as C++ wrt this.
    Rolf Magnus, Jul 4, 2003
    #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. kevin
    Replies:
    11
    Views:
    5,796
    Andrew McDonagh
    Jan 8, 2005
  2. Replies:
    8
    Views:
    426
  3. Arne Schmitz

    Pointer casting in GCC 4

    Arne Schmitz, Nov 21, 2005, in forum: C++
    Replies:
    7
    Views:
    340
    Arne Schmitz
    Nov 21, 2005
  4. Wally Barnes
    Replies:
    3
    Views:
    518
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    559
    James Kanze
    Dec 20, 2009
Loading...

Share This Page