This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Bug or no bug? gcc 2.95: Q: initializing non-const .. will use a temporary


Hello,


I have a question about whether this is or is not correct ansi C++.
What the code does, is passing a Foo object to generic_procedure
that accepts a non-const Bar.

This code does not compile using gcc 2.95, unless I use the
-fpermissive flag.

The error (or with -fpermissive, warning) I get is:
pass.cc: In function `int main()':
pass.cc:40: initializing non-const `Bar &' with `Foo' will use a
temporary
pass.cc:30: in passing argument 1 of `generic_procedure(Bar &)'

While I understand that the compiler should issue a warning,
because most of the time, using a temporary to solve this
problem is indeed definitely not what the user wants, in
this example, it is what I really want, because the
temporary that is created (Bar) will act as a shell object
around the argument supplied (Foo), and 'writing' to it
will not be lost.

To come back to my question: does the standard specifically
state that this is invalid C++ code, or is the gcc compiler
being too polite, and in this case, wrong in rejecting this
code ?

// code starts here

#include <iostream.h>

class Bar
{
  public:
    Bar(int *aData, int aLength)
      {
        data = aData;
        length = aLength;
        isMine = false;
      }
    
    int *data;
    int length;
    bool isMine;
};

class Foo
{
  public:
    int data[5];

    operator Bar ()
      {
        return Bar(data, 5);
      }
};

void generic_procedure(Bar& someBar) // line 30
{
  someBar.data[2] = 125;
}

int main()
{
  Foo foo;

  foo.data[0] = foo.data[1] = foo.data[2] = foo.data[3] = foo.data[4] =
25;

  generic_procedure(foo); // line 40

  cerr << foo.data[0] << " " << foo.data[1] << " " << foo.data[2]
       << " " << foo.data[3] << " " << foo.data[4];

  return 0;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]