Compiler warning when using vector of pointers

A

Adrian

Hi all,

I am getting a warning compiling the following code using Borland C++
Builder 6, but I dont think I am doing anything wrong. When using g++ I
get no warnings at all with a g++ -Wall -ansi -pedantic.

Now I usually assume the compiler is correct but in this case I am not sure.

push 1 gives the warning the other 2 go through fine.

The warning text is:-
"C:\temp>bcc32 a.cpp
Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland a.cpp:
Warning W8030 a.cpp 24: Temporary used for parameter '__x' in call to
'_STL::vector<A *,_STL::allocator<A *> >::push_back(A * const &)' in
function main()
Turbo Incremental Link 5.66 Copyright (c) 1997-2002 Borland"

I cant see why a temporary is being created, what am I missing

TIA


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

#include <vector>

class A
{
public:
virtual ~A()=0;
private:
};

A::~A()
{
}

class BA : public A
{
public:
private:
};

int main()
{
std::vector<A *> fred;

fred.push_back(new BA()); // push 1 Gives warning

A *ptr=new BA();
fred.push_back(ptr); // push 2
fred.push_back(ptr=new BA()); // push 3

return 0;
}
 
J

John Harrison

int main()
The return value from new is a temporary. Where else would the value be
stored? So push_back gets a reference to that temporary.

Are you sure, after all new is an operator returning a pointer.

What about this, does it give a warning?

fred.push_back(new A());

My guess is that the warning has something to do with the implicit
conversion that happens from BA* to A*.

But whatever, the warning is completely bogus and shouldn't be issued.
Compilers which give warnings on legitimate code are annoying because they
only encourage people to ignore compiler warning.

john
 
R

Rolf Magnus

Adrian said:
Hi all,

I am getting a warning compiling the following code using Borland C++
Builder 6, but I dont think I am doing anything wrong. When using g++ I
get no warnings at all with a g++ -Wall -ansi -pedantic.

Now I usually assume the compiler is correct but in this case I am not
sure.

push 1 gives the warning the other 2 go through fine.

The warning text is:-
"C:\temp>bcc32 a.cpp
Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland a.cpp:
Warning W8030 a.cpp 24: Temporary used for parameter '__x' in call to
'_STL::vector<A *,_STL::allocator<A *> >::push_back(A * const &)' in
function main()
Turbo Incremental Link 5.66 Copyright (c) 1997-2002 Borland"

I cant see why a temporary is being created, what am I missing

TIA


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

#include <vector>

class A
{
public:
virtual ~A()=0;
private:
};

A::~A()
{
}

class BA : public A
{
public:
private:
};

int main()
{
std::vector<A *> fred;

fred.push_back(new BA()); // push 1 Gives warning

The return value from new is a temporary. Where else would the value be
stored? So push_back gets a reference to that temporary.
A *ptr=new BA();
fred.push_back(ptr); // push 2

Here, you use a variable. push_back gets a reference to ptr.
fred.push_back(ptr=new BA()); // push 3

Here, the return value from new is assigned to ptr, and push_back gets a
reference to ptr.
 
A

Adrian

Rolf said:
The return value from new is a temporary. Where else would the value be
stored? So push_back gets a reference to that temporary.
Really, my copy of the C++ standard says its void *, please backup your
conclusion.

By the way
5.3.4.1 of the standard says
"If the entity is a non-array object, the new-expression returns a
pointer to the object created. If it is an array, the new-expression
returns a pointer to the initial element of the array."

If you dont know the answer dont answer my posts


TIA

Adrian
 
A

Adrian

John said:
What about this, does it give a warning?

fred.push_back(new A());
Change A to a non abstract class, and doing a push_back on the above I
dont get a warning.
My guess is that the warning has something to do with the implicit
conversion that happens from BA* to A*.

But whatever, the warning is completely bogus and shouldn't be issued.
Compilers which give warnings on legitimate code are annoying because they
only encourage people to ignore compiler warning.
I am glad you agree with me and its the compiler, I didn't think I was
doing anything wrong.


Thanks John


Adrian
 
R

Rob Williscroft

Adrian wrote in in comp.lang.c++:
Really, my copy of the C++ standard says its void *, please backup your
conclusion.

Either you don't have the real C++ Standard, or more likely you're
confusing the return from operator new () with the value of a new
expression.
By the way
5.3.4.1 of the standard says
"If the entity is a non-array object, the new-expression returns a
pointer to the object created. If it is an array, the new-expression
returns a pointer to the initial element of the array."

Indeed it return's a pointer of type "object *", *not* "void *",
its an rvalue, when bound to a "A * const &" it will be copied to
a temporary before a refrence to this temporary is passed to
std::vector said:
If you dont know the answer dont answer my posts

Whatever.

Rob.
 

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

Members online

Forum statistics

Threads
474,179
Messages
2,570,956
Members
47,509
Latest member
Jack116

Latest Threads

Top