473,486 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Compiler warning when using vector of pointers

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;
}
Jul 22 '05 #1
5 2489
Adrian wrote:
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.
return 0;
}


Jul 22 '05 #2
>> 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.


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
Jul 22 '05 #3
Rolf Magnus wrote:
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
Jul 22 '05 #4
John Harrison wrote:
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
Jul 22 '05 #5
Adrian wrote in news:cm**********@titan.btinternet.com in comp.lang.c++:
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.


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<>::push_back.
If you dont know the answer dont answer my posts


Whatever.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
11206
by: dis | last post by:
I've been going through my code and clearing away some of the compiler warnings that i'm generating and i've come across areas where i cast pointers to integer values. The Visual Studio compiler...
7
2659
by: Matthew Del Buono | last post by:
Don't try to solve the problem. I've found a way -- around or fixing it. I'm just curious as to whether this is Microsoft's problem in their compiler or if there's a standard saying this is to be...
2
2959
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
7
3315
by: Paul Sheer | last post by:
I need to automatically search and replace all fixed size buffer strcpy's with strncpy's (or better yet, strlcpy's) as a security and stability audit. The code base is large and it is not feasable...
1
3152
by: Hafeez | last post by:
I am having real trouble compiling this code http://www.cs.wisc.edu/~vganti/birchcode/codeHier/AttrProj.tgz The attachment shows errors when compiled using the current version of g++ in a...
29
2479
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
34
4828
by: Bob | last post by:
Hi, The compiler gives Warning 96 Variable 'cmdSource' is used before it has been assigned a value. A null reference exception could result at runtime. Dim cmdSource as SQlClient.SQLDataReader...
8
1328
by: Ramon F Herrera | last post by:
I have some code like this: RadioButtonGroup *empty_vector = new RadioButtonGroup(); ListOfRadioButton.push_back(*empty_vector); Coming from the Java camp, I keep on trying to write the above...
4
1827
by: Juha Nieminen | last post by:
Unknownmat wrote: VS2005 has a bug related to this. When you use size_t, it internally converts it to 'unsigned int'. In some situations it forgets that the type was actually size_t and only...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3066
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.