473,513 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Invalid initialization of non-const reference

#include <cstdlib>
#include <cctype>
#include <string>

template <class Input_Iter>
inline int IterToInt(Input_Iter& iter, const Input_Iter& lineEnd) {
char tempArray[80] = {0};
for (int i = 0; std::isdigit(*iter) && iter != lineEnd && i != 80;
++i) {
tempArray[i] = *iter;
++iter;
}
return atoi(tempArray);
}

int main(int argc, char* argv[]) {
if (argc > 1) {
std::string str = argv[1];
int a = IterToInt(str.begin(), str.end());
}

return 0;
}

A program I wrote does something similar to this and someone says that
it won't compile in GCC 3.4.2, which complains that:

error: invalid initialization of non-const reference of type
'__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >&' from a temporary of
type '__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >'

I don't use GCC, and so I hope that my test case above does actually
correctly demonstrate the problem. Should this fail? I didn't think
that my std::string str variable was temporary in the sense meant here?

Also, I realise that ideally IterToInt should use const_iterators, but
as far as I'm aware it isn't possible to do this in a generic way.

Thanks,

James

Jul 23 '05 #1
5 11431
* James Gregory:
#include <cstdlib>
#include <cctype>
#include <string>

template <class Input_Iter>
inline int IterToInt(Input_Iter& iter, const Input_Iter& lineEnd) {
char tempArray[80] = {0};
for (int i = 0; std::isdigit(*iter) && iter != lineEnd && i != 80;
++i) {
tempArray[i] = *iter;
++iter;
}
return atoi(tempArray);
}

int main(int argc, char* argv[]) {
if (argc > 1) {
std::string str = argv[1];
int a = IterToInt(str.begin(), str.end());
}

return 0;
}

A program I wrote does something similar to this and someone says that
it won't compile in GCC 3.4.2, which complains that:

error: invalid initialization of non-const reference of type
'__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >&' from a temporary of
type '__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >'

I don't use GCC, and so I hope that my test case above does actually
correctly demonstrate the problem. Should this fail?
Yes.

I didn't think
that my std::string str variable was temporary in the sense meant here?
It isn't, but the iterators are temporary.

Also, I realise that ideally IterToInt should use const_iterators, but
as far as I'm aware it isn't possible to do this in a generic way.


Why do you pass by reference?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
James Gregory wrote:
#include <cstdlib>
#include <cctype>
#include <string>

template <class Input_Iter>
inline int IterToInt(Input_Iter& iter, const Input_Iter& lineEnd) {
char tempArray[80] = {0};
for (int i = 0; std::isdigit(*iter) && iter != lineEnd && i != 80;
++i) {
tempArray[i] = *iter;
++iter;
}
return atoi(tempArray);
}

int main(int argc, char* argv[]) {
if (argc > 1) {
std::string str = argv[1];
int a = IterToInt(str.begin(), str.end());
}

return 0;
}

A program I wrote does something similar to this and someone says that
it won't compile in GCC 3.4.2, which complains that:

error: invalid initialization of non-const reference of type
'__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >&' from a temporary of
type '__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >'

I don't use GCC, and so I hope that my test case above does actually
correctly demonstrate the problem. Should this fail?
Yes.
I didn't think that my std::string str variable was temporary in the sense
meant here?
No. How could '__gnu_cxx::__normal_iterator<...>' be your std::string?
Anyway, your iterators are temopraries. str.begin() returns a temporary
iterator that you try to bind to a non-const reference in IterToInt. And
C++ doesn't allow this.
Also, I realise that ideally IterToInt should use const_iterators, but
as far as I'm aware it isn't possible to do this in a generic way.


It uses whatever you supply as argument.

Jul 23 '05 #3

Alf P. Steinbach wrote:
* James Gregory:
#include <cstdlib>
#include <cctype>
#include <string>

template <class Input_Iter>
inline int IterToInt(Input_Iter& iter, const Input_Iter& lineEnd) {
char tempArray[80] = {0};
for (int i = 0; std::isdigit(*iter) && iter != lineEnd && i != 80; ++i) {
tempArray[i] = *iter;
++iter;
}
return atoi(tempArray);
}

int main(int argc, char* argv[]) {
if (argc > 1) {
std::string str = argv[1];
int a = IterToInt(str.begin(), str.end());
}

return 0;
}

A program I wrote does something similar to this and someone says that it won't compile in GCC 3.4.2, which complains that:

error: invalid initialization of non-const reference of type
'__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >&' from a temporary of type '__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >'

I don't use GCC, and so I hope that my test case above does actually correctly demonstrate the problem. Should this fail?
Yes.

I didn't think
that my std::string str variable was temporary in the sense meant here?
It isn't, but the iterators are temporary.


Oh, yeah, doh, thanks.
Also, I realise that ideally IterToInt should use const_iterators, but as far as I'm aware it isn't possible to do this in a generic way.


Why do you pass by reference?


I guess iterators are small enough that passing them by value is as
fast/faster than passing them by reference? However, rather than
agonising over every argument to very function to decide whether it
would be faster to pass by value or by reference, I just go by a rule
of thumb that everything is passed by reference unless either a) it's a
built in type or b) I particularly don't want the original altered.

Thanks,

James

Jul 23 '05 #4
Plus of course if it's an inline function I realise that nothing
actually really gets passed by value in any case.

Jul 23 '05 #5
James Gregory wrote:

Alf P. Steinbach wrote:
* James Gregory:
> #include <cstdlib>
> #include <cctype>
> #include <string>
>
> template <class Input_Iter>
> inline int IterToInt(Input_Iter& iter, const Input_Iter& lineEnd) {
> char tempArray[80] = {0};
> for (int i = 0; std::isdigit(*iter) && iter != lineEnd && i != 80; > ++i) {
> tempArray[i] = *iter;
> ++iter;
> }
> return atoi(tempArray);
> }
>
> int main(int argc, char* argv[]) {
> if (argc > 1) {
> std::string str = argv[1];
> int a = IterToInt(str.begin(), str.end());
> }
>
> return 0;
> }
>
> A program I wrote does something similar to this and someone says that > it won't compile in GCC 3.4.2, which complains that:
>
> error: invalid initialization of non-const reference of type
> '__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
> std::char_traits<char>, std::allocator<char> > >&' from a temporary of > type '__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
> std::char_traits<char>, std::allocator<char> > >'
>
> I don't use GCC, and so I hope that my test case above does actually > correctly demonstrate the problem. Should this fail?
Yes.

> I didn't think
> that my std::string str variable was temporary in the sense meant here?

It isn't, but the iterators are temporary.


Oh, yeah, doh, thanks.
> Also, I realise that ideally IterToInt should use const_iterators, but > as far as I'm aware it isn't possible to do this in a generic way.


Why do you pass by reference?


I guess iterators are small enough that passing them by value is as
fast/faster than passing them by reference?


Possibly.
However, rather than agonising over every argument to very function to
decide whether it would be faster to pass by value or by reference, I just
go by a rule of thumb that everything is passed by reference unless either
a) it's a built in type or b) I particularly don't want the original
altered.


This is not about performance, but about the fact that you _do_ alter the
original, which you are not allowed to if that original is a temporary.
Passing by value instead of by reference, you wouldn't have the problem you
have now, and in fact, it's common to pass iterators by value.

Jul 23 '05 #6

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

Similar topics

2
4013
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
10
4163
by: JKop | last post by:
What's the difference between them? Take the following: #include <iostream> struct Blah { int k;
4
2177
by: Bret Pehrson | last post by:
I just stumbled across the following problem: //.h class Masses { static double mass1; static double mass2; static double mass3; };
6
2153
by: anongroupaccount | last post by:
class CustomType { public: CustomType(){_i = 0;} CustomType(int i) : _i(i) {} private: int _i; }; class MyClass
10
8504
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
8
3101
by: lovecreatesbea... | last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the initialization is done once only, conceptually before the program starts executing, ... . "Non-automatic variables are...
7
14928
by: The|Godfather | last post by:
Hi everybody, I read Scotte Meyer's "Effective C++" book twice and I know that he mentioned something specific about constructors and destructors that was related to the following...
23
3627
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
3693
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
20
6066
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
0
7177
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
7394
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
7559
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
7123
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
7542
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
5701
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,...
1
5100
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...
0
1611
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
470
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.