#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(*i ter) && 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.b egin(), 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::__n ormal_iterator< char*, std::basic_stri ng<char,
std::char_trait s<char>, std::allocator< char> > >&' from a temporary of
type '__gnu_cxx::__n ormal_iterator< char*, std::basic_stri ng<char,
std::char_trait s<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 5 11438
* 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(*i ter) && 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.b egin(), 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::__n ormal_iterator< char*, std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char> > >&' from a temporary of type '__gnu_cxx::__n ormal_iterator< char*, std::basic_stri ng<char, std::char_trait s<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?
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(*i ter) && 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.b egin(), 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::__n ormal_iterator< char*, std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char> > >&' from a temporary of type '__gnu_cxx::__n ormal_iterator< char*, std::basic_stri ng<char, std::char_trait s<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::__n ormal_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.
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(*i ter) && 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.b egin(), 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::__n ormal_iterator< char*, std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char> > >&' from a temporary
of type '__gnu_cxx::__n ormal_iterator< char*, std::basic_stri ng<char, std::char_trait s<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
Plus of course if it's an inline function I realise that nothing
actually really gets passed by value in any case.
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(*i ter) && 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.b egin(), 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::__n ormal_iterator< char*, std::basic_stri ng<char, > std::char_trait s<char>, std::allocator< char> > >&' from a temporary of > type '__gnu_cxx::__n ormal_iterator< char*, std::basic_stri ng<char, > std::char_trait s<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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
I am not fully convinced that this problem cannot be
solved. I am going to propose a solution here. For the sake of
discussion I will post my...
|
by: JKop |
last post by:
What's the difference between them?
Take the following:
#include <iostream>
struct Blah
{
int k;
|
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;
};
|
by: anongroupaccount |
last post by:
class CustomType
{
public:
CustomType(){_i = 0;}
CustomType(int i) : _i(i) {}
private:
int _i;
};
class MyClass
|
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,
| |
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 initialized before the program starts
executing." -- What does this mean? What is the name of the stage in
which the mentioned initialization is...
|
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 error/warning: "error: invalid use of nonstatic data member "
However, he did NOT mention this error in the book explicitly.It
happens always in the...
|
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 the built-in types,
the value is usually garbage. Is this right?
However, I'm a bit confused about value-initialization, when does it
happen, and...
|
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 value-initialization. I
think default-init calls default constructor for class objects and
sets garbage values to PODs. Value-init also calls default
constructor for...
|
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.) main().
So, if the above is OK, does static initialization occur during A or B? What
happens during A?
|
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. ...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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...
|
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...
|
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...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |