J
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 = *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
#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 = *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