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

H

hrmadhu

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

#include<vector>
#include<deque>
#include<iostream>
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++;
}
// some stuff

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

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

// Second Attempt
vector<deque<int> > _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<int> > _MyVar (N);
?
Thanks In Advance.
Best Regards,
Madhu.
 
J

Jeff Schwab

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

#include<vector>
#include<deque>
#include<iostream>
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++;
}
// some stuff

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

// First attempt
vector<deque<int> > _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<int> > _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<int> > _MyVar (N);
?

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

Good luck,
Jeff
 
H

hrmadhu

Jeff Schwab said:
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.
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 ?
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.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
 
J

Jeff Schwab

hrmadhu said:
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.

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.
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.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
 
H

hrmadhu

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 said:
hrmadhu said:
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.

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.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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top