non-const reference to temporary derived from pure virtual Base

Discussion in 'C++' started by reppisch, May 6, 2008.

  1. reppisch

    reppisch Guest

    Hi Ng,

    besides that this style may be asking for trouble i'm faced with a
    unexpected gcc behaviour. Tested with gcc 3.3.3.
    On M$ .Net this compiles just fine.

    I'm trying to make a non-const reference to a temporary object. The
    object is derived from a pure virtual base, so the compiler can't create
    a copy of base-type.

    Why does this not work with gcc?

    To reproduce:
    //-----------------------------------
    class Base {
    public:
    Base() {};
    virtual void pv() = 0;
    virtual ~Base() {};
    };

    class Derived : public Base {
    public:
    Derived(int i) {};
    virtual void pv() {};
    virtual ~Derived() {};
    };

    class User {
    public:
    void useBase(Base& b) {};
    void useBaseConst(const Base& b) {};
    };


    int main() {
    User u;
    Derived d(1);


    // Ok with non-temporary !
    u.useBase(d);

    // not ok!
    u.useBase(Derived(1));
    //* no matching function for call to `User::useBase(Derived)'
    //* candidates are: void User::useBase(Base&)
    // seems that the compiler refuses to make a
    // non-const reference to a temporary

    // but this is ok!
    u.useBaseConst(Derived(1));

    }

    Regards,

    Michael
     
    reppisch, May 6, 2008
    #1
    1. Advertising

  2. reppisch schrieb:
    > // not ok!
    > u.useBase(Derived(1));
    > //* no matching function for call to `User::useBase(Derived)'
    > //* candidates are: void User::useBase(Base&)
    > // seems that the compiler refuses to make a
    > // non-const reference to a temporary


    Exactly that. Temporaries are like rvalues.

    I had a similar question some time ago (maybe in the german group).
    Somebody explained me that this is intended by the standard, because
    there are risks otherwise.

    You must create a non-temporary object for this purpuse.

    Derived d(1);
    u.useBase(d);

    Marcel
     
    Marcel Müller, May 6, 2008
    #2
    1. Advertising

  3. reppisch

    reppisch Guest

    Hi,

    i striped it down further to:

    class Derived {
    public:
    Derived(int i) {};
    };

    class User {
    public:
    void useBase(Derived& b) {};
    };

    int main() {
    User u;
    u.useBase(Derived(1));
    }

    I think it's just a "feature" to prevent writing code with surprising
    results....
     
    reppisch, May 6, 2008
    #3
  4. On 2008-05-06 16:41, reppisch wrote:
    > Hi Ng,
    >
    > besides that this style may be asking for trouble i'm faced with a
    > unexpected gcc behaviour. Tested with gcc 3.3.3.
    > On M$ .Net this compiles just fine.
    >
    > I'm trying to make a non-const reference to a temporary object. The
    > object is derived from a pure virtual base, so the compiler can't create
    > a copy of base-type.


    Are you sure VS.NET allowed that? If that is the case you really should
    check the options you provide to the compiler because making a non-const
    reference to a temporary is forbidden by the standard.

    --
    Erik Wikström
     
    Erik Wikström, May 6, 2008
    #4
  5. reppisch

    sk_usenet Guest

    "Erik Wikström" <> wrote in message
    > On 2008-05-06 16:41, reppisch wrote:
    >> Hi Ng,
    >>
    >> besides that this style may be asking for trouble i'm faced with a
    >> unexpected gcc behaviour. Tested with gcc 3.3.3.
    >> On M$ .Net this compiles just fine.
    >>
    >> I'm trying to make a non-const reference to a temporary object. The
    >> object is derived from a pure virtual base, so the compiler can't create
    >> a copy of base-type.

    >
    > Are you sure VS.NET allowed that? If that is the case you really should
    > check the options you provide to the compiler because making a non-const
    > reference to a temporary is forbidden by the standard.


    Yes, by default VC allows this as an extension to Standrad (I tested a long
    time back on probably v7.0). I think it's the /Za option in VC compiler that
    would flash the error.

    --
    http://techytalk.googlepages.com
     
    sk_usenet, May 6, 2008
    #5
  6. reppisch wrote:
    > ...
    > i striped it down further to:
    > ...


    You can strip it down to

    void foo(int&);
    foo(1);

    But the bottom line is that explicit binding of non-const references to
    temporary objects is illegal in C++.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, May 6, 2008
    #6
  7. sk_usenet wrote:
    > ...
    > Yes, by default VC allows this as an extension to Standrad (I tested a long
    > time back on probably v7.0). I think it's the /Za option in VC compiler that
    > would flash the error.
    > ...


    Yes, /Za will fix it.

    Without /Za in VS2005 even they implemented a mix of standard behavior
    and extended behavior so that the whole thing does seem to satisfy the
    formal definition of "compiler extension", while the VC6 was genuinely
    and hopelessly broken in that regard.

    A plug: see item 2.3 in
    http://atarasevich.blogspot.com/2008/02/microsoft-vs2005-c-non-compliance_07.html

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, May 6, 2008
    #7
    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. Gianni Mariani
    Replies:
    2
    Views:
    520
    Gianni Mariani
    Aug 27, 2003
  2. jeffc
    Replies:
    0
    Views:
    372
    jeffc
    Aug 27, 2003
  3. Replies:
    4
    Views:
    440
    Alf P. Steinbach
    May 23, 2007
  4. Replies:
    1
    Views:
    425
    myork
    May 23, 2007
  5. Replies:
    1
    Views:
    413
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page