473,769 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help on the STL and initializing non-const references with temporaries

Hi,
I wish to declare a vector of deque of int, which I do as follows.

#include<vector >
#include<deque>
#include<iostre am>
using namespace std;

int main(int argc, char* argv[])
{
int i=1;
int N=0;
while(i<=argc)
{
// parse arguments
// Most importantly extract the value of N
// as
N=atoi(argv[i]);
i++;
}
// some stuff

//declare a vector of deque of int
// vector contains N deque<int>
// each of which are empty initially

// First attempt
vector<deque<in t> > _MyVariable( N, deque<int>());

// Second Attempt
vector<deque<in t> > _MyVariable2 (N, deque<int>(0));

// some more stuff
return 0;
}
The compiler cribs

"Warning: should not initialize a non-const reference with a
temporary." in the STL code which is instantiated in the lines of
interest above.

Could someone please clarify the exact effects of initializing in the
above fashion ?
which of the above two attempts should I use ? Is it better not to
initialize the deque i.e. to use
vector<deque<in t> > _MyVar (N);
?
Thanks In Advance.
Best Regards,
Madhu.
Jul 22 '05 #1
4 1759
hrmadhu wrote:
Hi,
I wish to declare a vector of deque of int, which I do as follows.

#include<vector >
#include<deque>
#include<iostre am>
using namespace std;

int main(int argc, char* argv[])
{
int i=1;
int N=0;
while(i<=argc)
{
// parse arguments
// Most importantly extract the value of N
// as
N=atoi(argv[i]);
i++;
}
// some stuff

//declare a vector of deque of int
// vector contains N deque<int>
// each of which are empty initially

// First attempt
vector<deque<in t> > _MyVariable( N, deque<int>());
You're not allowed (in standard C++) to use that name. At least get rid
of the underscore.

// Second Attempt
vector<deque<in t> > _MyVariable2 (N, deque<int>(0));

// some more stuff
return 0;
You don't need that return statement.
}
The compiler cribs

"Warning: should not initialize a non-const reference with a
temporary." in the STL code which is instantiated in the lines of
interest above.

Could someone please clarify the exact effects of initializing in the
above fashion ?
which of the above two attempts should I use ? Is it better not to
initialize the deque i.e. to use
vector<deque<in t> > _MyVar (N);
?
Yes, IMHO. That's the right way to do it, assuming you change the
variable name.

Good luck,
Jeff
Thanks In Advance.
Best Regards,
Madhu.


Jul 22 '05 #2
>Jeff Schwab <je******@comca st.net> wrote in message news:
// First attempt
vector<deque<in t> > _MyVariable( N, deque<int>());


You're not allowed (in standard C++) to use that name. At least get rid
of the underscore.


Wow!! I didnt know that. As my coding style, I always prefix all
private and protected members of my classes with _ . Could you please
tell me why and where I can find more on this.

// Second Attempt
vector<deque<in t> > _MyVariable2 (N, deque<int>(0));

// some more stuff
return 0;


You don't need that return statement.


if I do not put this, then the compiler cribs that main, which is
declared int main(...) does not have a return value. Also, isnt that
the way I return whether the program executed with or without errors ?
return 0 suggests that the program ran successfully, while return -1
says there was an error ?
}
The compiler cribs

"Warning: should not initialize a non-const reference with a
temporary." in the STL code which is instantiated in the lines of
interest above.

Could someone please clarify the exact effects of initializing in the
above fashion ?
which of the above two attempts should I use ? Is it better not to
initialize the deque i.e. to use
vector<deque<in t> > _MyVar (N);
?


Yes, IMHO. That's the right way to do it, assuming you change the
variable name.


if so, can I then simply use the variable as
_MyVar[i].push_back() ?
or do I need to create non-temporary deque<int> and push them back
into the vector one by one ?
Thanks again
Madhu
Jul 22 '05 #3
hrmadhu wrote:
Jeff Schwab <je******@comca st.net> wrote in message news:
// First attempt
vector<deque<in t> > _MyVariable( N, deque<int>());
You're not allowed (in standard C++) to use that name. At least get rid
of the underscore.

Wow!! I didnt know that. As my coding style, I always prefix all
private and protected members of my classes with _ . Could you please
tell me why and where I can find more on this.


TC++PL, p.81:

Names starting with an underscore are reserved for
special facilities in the implementation and the run-
time environment, so such names should not be used in
application programs.

// Second Attempt
vector<deque<in t> > _MyVariable2 (N, deque<int>(0));

// some more stuff
return 0;


You don't need that return statement.

if I do not put this, then the compiler cribs that main, which is
declared int main(...) does not have a return value.


Your compiler is non-standard. In standard C++, the default return
value of main is 0. You are correct to declare it returning an int.
Also, isnt that
the way I return whether the program executed with or without errors ?
return 0 suggests that the program ran successfully, while return -1
says there was an error ?
That depends on your system. On Unix, you can return whatever you want,
as long as you document it. There is a sort of gentlemen's agreement
that 0 means success.
}

The compiler cribs

"Warning: should not initialize a non-const reference with a
temporary. " in the STL code which is instantiated in the lines of
interest above.

Could someone please clarify the exact effects of initializing in the
above fashion ?
which of the above two attempts should I use ? Is it better not to
initialize the deque i.e. to use
vector<deque <int> > _MyVar (N);
?


Yes, IMHO. That's the right way to do it, assuming you change the
variable name.

if so, can I then simply use the variable as
_MyVar[i].push_back() ?


Assuming you change the variable name, and 0 <= i < N, yes.
or do I need to create non-temporary deque<int> and push them back
into the vector one by one ?
No, but you can if you like.
Thanks again
Madhu


-Jeff

Jul 22 '05 #4
Thanks Jeff. Got it compiling without any error. (in fact it was also
running without error - but I just dislike compiler warnings!!)
Thanks again.
Best Regards,
Madhu.

Jeff Schwab <je******@comca st.net> wrote in message news:<1K******* *************@c omcast.com>...
hrmadhu wrote:
Jeff Schwab <je******@comca st.net> wrote in message news:

// First attempt
vector<deque<in t> > _MyVariable( N, deque<int>());

You're not allowed (in standard C++) to use that name. At least get rid
of the underscore.

Wow!! I didnt know that. As my coding style, I always prefix all
private and protected members of my classes with _ . Could you please
tell me why and where I can find more on this.


TC++PL, p.81:

Names starting with an underscore are reserved for
special facilities in the implementation and the run-
time environment, so such names should not be used in
application programs.

// Second Attempt
vector<deque<in t> > _MyVariable2 (N, deque<int>(0));

// some more stuff
return 0;

You don't need that return statement.

if I do not put this, then the compiler cribs that main, which is
declared int main(...) does not have a return value.


Your compiler is non-standard. In standard C++, the default return
value of main is 0. You are correct to declare it returning an int.
Also, isnt that
the way I return whether the program executed with or without errors ?
return 0 suggests that the program ran successfully, while return -1
says there was an error ?


That depends on your system. On Unix, you can return whatever you want,
as long as you document it. There is a sort of gentlemen's agreement
that 0 means success.
}

The compiler cribs

"Warning: should not initialize a non-const reference with a
temporary. " in the STL code which is instantiated in the lines of
interest above.

Could someone please clarify the exact effects of initializing in the
above fashion ?
which of the above two attempts should I use ? Is it better not to
initialize the deque i.e. to use
vector<deque <int> > _MyVar (N);
?

Yes, IMHO. That's the right way to do it, assuming you change the
variable name.

if so, can I then simply use the variable as
_MyVar[i].push_back() ?


Assuming you change the variable name, and 0 <= i < N, yes.
or do I need to create non-temporary deque<int> and push them back
into the vector one by one ?


No, but you can if you like.
Thanks again
Madhu


-Jeff

Jul 22 '05 #5

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

Similar topics

50
6377
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
13
27164
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
3
1270
by: Steve Graddy | last post by:
I am trying to convert the following VB.Net code to C# and I am getting the compiler error: VB.NET code: '' Create a delegate that will be called asynchronously Private Delegate Function GetTextData(ByVal DatabaseName As String, _ByVal ProcName As String) Dim async As New GetTextData(AddressOf TextProxy)
17
2623
by: Calle Pettersson | last post by:
Coming from writing mostly in Java, I have trouble understanding how to declare a member without initializing it, and do that later... In Java, I would write something like public static void main(String args) { MyType aMember; ... aMember = new MyType(...) ... } However, in C++ this does not seem to work. I declare in class (it's
2
3383
by: teddybyte | last post by:
my script below is: #include "stdafx.h" int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
2
2903
by: rookiejavadude | last post by:
I'm have most of my java script done but can not figure out how to add a few buttons. I need to add a delete and add buttong to my existing java program. Not sure were to add it on how. Can anyone help? my script is below. thank you import java.awt.*; //import all java.awt import java.awt.event.*; //import all java.awt.event import java.util.*; //import all java.util import javax.swing.*; //import all javax.swing class Product...
4
2716
by: Polar | last post by:
Hello everyone! I'm new here. I am doing a project, Digital Compass Navigation Aids. It consists of the 1490 Digital Compass, a P18F4620 Microcontroller, ISD2560 voice record/playback chip LM4808M amplifier, 5volts and 3.3volts voltage regulators and three switches, the recording switch, the playback switch. First, i should record north, south, east and west into the chip by pressing SW3. Then by pressing SW4, it should playback my voice. ...
10
1910
by: Jason Doucette | last post by:
Situation: I have a simple struct that, say, holds a color (R, G, and B). I created my own constructors to ease its creation. As a result, I lose the default constructor. I dislike this, but it's easy to solve: I just make my own default constructor. Problem: My own default constructor is considered to be *initializing the variable* (even though it doesn't), whereas the original one does not. Thus, when I declare and use it before...
13
2340
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other initialization takes place." Does this mean all non-local objects will be zero-initialized before they are initialized by their initializers(if they have)? For example: int g_var = 3; int main() {}
6
4379
by: Jai Prabhu | last post by:
Hi All, Consider the following piece of code: void func (void) { static unsigned char arr = "\x00\xAA\xBB"; fprintf (stderr, "0x%x\n", arr); fprintf (stderr, "0x%x\n", arr);
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3965
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 we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.