Need help understanding why this invalid C++ code compiles successfully - c++

The following code compile correctly for me in some environments (e.g. on compiler explorer with GCC 9.3.0) and complains in others (CentOS 7.9.2009 (Core), GCC 9.3.1).
#include <iostream>
#include <string>
using namespace std;
int main() {
std::string name="aakash";
name.erase(name.begin()+2, name.cend());
return 0;
}
When I get an error, the error is:
test.cpp:6:40: error: no matching function for call to 'std::basic_string<char>::erase(__gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, std::basic_string<char>::const_iterator)'
6 | name.erase(name.begin()+2, name.cend());
| ^
In file included from /opt/rh/devtoolset-9/root/usr/include/c++/9/string:55,
from /opt/rh/devtoolset-9/root/usr/include/c++/9/bits/locale_classes.h:40,
from /opt/rh/devtoolset-9/root/usr/include/c++/9/bits/ios_base.h:41,
from /opt/rh/devtoolset-9/root/usr/include/c++/9/ios:42,
from /opt/rh/devtoolset-9/root/usr/include/c++/9/ostream:38,
from /opt/rh/devtoolset-9/root/usr/include/c++/9/iostream:39,
from test.cpp:1:
/opt/rh/devtoolset-9/root/usr/include/c++/9/bits/basic_string.h:4698:7: note: candidate: 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::erase(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]'
4698 | erase(size_type __pos = 0, size_type __n = npos)
| ^~~~~
/opt/rh/devtoolset-9/root/usr/include/c++/9/bits/basic_string.h:4698:23: note: no known conversion for argument 1 from '__gnu_cxx::__normal_iterator<char*, std::basic_string<char> >' to 'std::basic_string<char>::size_type' {aka 'long unsigned int'}
4698 | erase(size_type __pos = 0, size_type __n = npos)
| ~~~~~~~~~~^~~~~~~~~
/opt/rh/devtoolset-9/root/usr/include/c++/9/bits/basic_string.h:4714:7: note: candidate: 'std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string<_CharT, _Traits, _Alloc>::erase(std::basic_string<_CharT, _Traits, _Alloc>::iterator) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; typename _Alloc::rebind<_CharT>::other::pointer = char*]'
4714 | erase(iterator __position)
| ^~~~~
/opt/rh/devtoolset-9/root/usr/include/c++/9/bits/basic_string.h:4714:7: note: candidate expects 1 argument, 2 provided
/opt/rh/devtoolset-9/root/usr/include/c++/9/bits/basic_string.h:4734:7: note: candidate: 'std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string<_CharT, _Traits, _Alloc>::erase(std::basic_string<_CharT, _Traits, _Alloc>::iterator, std::basic_string<_CharT, _Traits, _Alloc>::iterator) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; typename _Alloc::rebind<_CharT>::other::pointer = char*]'
4734 | erase(iterator __first, iterator __last);
| ^~~~~
/opt/rh/devtoolset-9/root/usr/include/c++/9/bits/basic_string.h:4734:40: note: no known conversion for argument 2 from '__normal_iterator<const char*,[...]>' to '__normal_iterator<char*,[...]>'
4734 | erase(iterator __first, iterator __last);
| ~~~~~~~~~^~~~~~
| ~~~~~~~~~^~~~~~
The error looks reasonable to me because, as per C++ documentation, both arguments to std::basic_string::erase should either be of type iterator or const_iterator.
So, when it works (e.g. here), what allows it to work?

Since C++11, both arguments to the two-iterator overload of std::string::erase() are const_iterator. See form #3 on this cppreference page (the one you linked in your question).
Further, the C++11 Standard requires that a conatiner's iterator types are convertible to the equivalent const_iterator.
From this Draft C++11 Standard, in §23.2.1 [container.requirements.general], Table 96 has the following entry (bold emphasis mine):
Expression
Return Type
Operational Semantics
Assertion/note
…
X::iterator
iterator type whose value type is T
any iterator category that meets the forward iterator requirements. convertible to X::const_iterator.
…
So, in your call to .erase(), the first argument is being converted to a string::const_iterator.
Thus, the bug is in those compilers that don't accept your call, assuming you have set them also to use the C++11 (or later) standard.

Related

Cannot compile with TSDuck in GCC 8.3.1

TSDuck Version 3.27.2438
GCC Version 8.3.1
Centos 7.9
I am trying to compile some code against TSDuck (RPM install version 3.27-2438). I get a compile error on UString.h. GCC seems to be unhappy with the use of a const_iterator in the erase function. As I understand it, this has been allowed since C++11 and should be implemented in GCC 8.3.1. Has anyone else run into this issue? It seems like a compiler bug, but I've also tried it in GCC 9.3.1 and gotten the same error.
Error:
/usr/include/tsduck/tsUString.h:2088:84: error: no matching function for call to ‘ts::UString::erase(std::basic_string<char16_t>::const_iterator&)’
iterator erase(const_iterator position) { return SuperClass::erase(position); }
^
In file included from /opt/rh/devtoolset-8/root/usr/include/c++/8/string:52,
from /opt/rh/devtoolset-8/root/usr/include/c++/8/stdexcept:39,
from /opt/rh/devtoolset-8/root/usr/include/c++/8/array:39,
from /opt/rh/devtoolset-8/root/usr/include/c++/8/tuple:39,
from /opt/rh/devtoolset-8/root/usr/include/c++/8/bits/unique_ptr.h:37,
from /opt/rh/devtoolset-8/root/usr/include/c++/8/memory:80,
from /workspaces/ts_reader/src/ts_reader/ts_reader.cpp:4:
/opt/rh/devtoolset-8/root/usr/include/c++/8/bits/basic_string.h:4632:7: note: candidate: ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::erase(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
erase(size_type __pos = 0, size_type __n = npos)
^~~~~
/opt/rh/devtoolset-8/root/usr/include/c++/8/bits/basic_string.h:4632:7: note: no known conversion for argument 1 from ‘std::basic_string<char16_t>::const_iterator’ {aka ‘__gnu_cxx::__normal_iterator<const char16_t*, std::basic_string<char16_t> >’} to ‘std::basic_string<char16_t>::size_type’ {aka ‘long unsigned int’}
/opt/rh/devtoolset-8/root/usr/include/c++/8/bits/basic_string.h:4648:7: note: candidate: ‘std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string<_CharT, _Traits, _Alloc>::erase(std::basic_string<_CharT, _Traits, _Alloc>::iterator) [with _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator<char16_t*, std::basic_string<char16_t> >; typename _Alloc::rebind<_CharT>::other::pointer = char16_t*]’
erase(iterator __position)
^~~~~
/opt/rh/devtoolset-8/root/usr/include/c++/8/bits/basic_string.h:4648:7: note: no known conversion for argument 1 from ‘__normal_iterator<const char16_t*,[...]>’ to ‘__normal_iterator<char16_t*,[...]>’
/opt/rh/devtoolset-8/root/usr/include/c++/8/bits/basic_string.h:4668:7: note: candidate: ‘std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string<_CharT, _Traits, _Alloc>::erase(std::basic_string<_CharT, _Traits, _Alloc>::iterator, std::basic_string<_CharT, _Traits, _Alloc>::iterator) [with _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator<char16_t*, std::basic_string<char16_t> >; typename _Alloc::rebind<_CharT>::other::pointer = char16_t*]’
erase(iterator __first, iterator __last);
^~~~~
Check that you are using the C++11 ABI.
https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
_GLIBCXX_USE_CXX11_ABI
Defined to the value 1 by default. Configurable via --disable-libstdcxx-dual-abi and/or --with-default-libstdcxx-abi. ABI-changing. When defined to a non-zero value the library headers will use the new C++11-conforming ABI introduced in GCC 5, rather than the older ABI introduced in GCC 3.4. This changes the definition of several class templates, including std:string, std::list and some locale facets. For more details see Dual ABI.

Why boost::replace_all_copy is not working? C++

I am trying to make working code to replace text of line. Using boost_1_64_0 .
getline (ifs, line);
cout << line << endl;
// 1.
boost::replace_all(line, "abc", "hij");
boost::replace_all(line, "def", "klm");
// 2.
line = boost::replace_all_copy
boost::replace_all_copy<string>
(
("abc def abc def", "abc", "hij")
, "def"
, "klm"
);
I am getting these errors:
main.cpp||In function 'int main()':|
main.cpp|37|error: no match for 'operator=' in 'line = boost::algorithm::replace_all_copy'|
main.cpp|37|note: candidates are:|
\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|543|note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]|
mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|543|note: no known conversion for argument 1 from '<unresolved overloaded function type>' to 'const std::basic_string<char>&'|
mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|551|note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]|
mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|551|note: no known conversion for argument 1 from '<unresolved overloaded function type>' to 'const char*'|
mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|562|note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]|
mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|562|note: no known conversion for argument 1 from '<unresolved overloaded function type>' to 'char'|
main.cpp|38|error: expected ';' before 'boost'|
main.cpp|53|error: expected '}' before 'else'|
Why the error happens or how can I correct them?
You have written obviously broken code:
line = boost::replace_all_copy boost::replace_all_copy<string>(...)

Why does istream_iterator<string>(ifstream("test.txt")) cause an error?

I have tried to write a code to read strings from file named "test.txt" and write the strings to standard output. The code below works well:
int main()
{
using namespace std;
ifstream file("test.txt");
copy(istream_iterator<string>(file),
istream_iterator<string>(),
ostream_iterator<string>(cout, " "));
}
However, with this modification, the code no longer compiles:
int main()
{
using namespace std;
copy(istream_iterator<string>(ifstream("test.txt")), // <-- Error here
istream_iterator<string>(),
ostream_iterator<string>(cout, " "));
}
Why doesn't this version compile?
The compiler I used is g++4.6.2, and the error as below:
ex11-16.cpp:16:65: error: no matching function for call to 'std::istream_iterator<std::basic_string<char> >::istream_iterator(std::ifstream)'
ex11-16.cpp:16:65: note: candidates are:
.../bits/stream_iterator.h:72:7: note: std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator(const std::istream_iterator<_Tp, _CharT, _Traits, _Dist>&) [with _Tp = std::basic_string<char>, _CharT = char, _Traits = std::char_traits<char>, _Dist = int, std::istream_iterator<_Tp, _CharT, _Traits, _Dist> = std::istream_iterator<std::basic_string<char> >]
.../bits/stream_iterator.h:72:7: note: no known conversion for argument 1 from 'std::ifstream {aka std::basic_ifstream<char>}' to 'const std::istream_iterator<std::basic_string<char> >&'
.../bits/stream_iterator.h:68:7: note: std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator(std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_type&) [with _Tp = std::basic_string<char>, _CharT = char, _Traits = std::char_traits<char>, _Dist = int, std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_type = std::basic_istream<char>]
.../bits/stream_iterator.h:68:7: note: no known conversion for argument 1 from 'std::ifstream {aka std::basic_ifstream<char>}' to 'std::istream_iterator<std::basic_string<char> >::istream_type& {aka std::basic_istream<char>&}'
.../bits/stream_iterator.h:64:26: note: std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator() [with _Tp = std::basic_string<char>, _CharT = char, _Traits = std::char_traits<char>, _Dist = int]
.../bits/stream_iterator.h:64:26: note: candidate expects 0 arguments, 1 provided
There are (I believe) two errors here:
(1) You need to put quotations around test.txt:
istream_iterator<string>(ifstream("test.txt"), ... );
(2) istream_iterator's constructor takes in an istream& (that is, an lvalue reference to a stream). Consequently, the stream that you pass in has to be an lvalue. However, passing in ifstream("test.txt") passes in a temporary object of type ifstream, which is an rvalue rather than an lvalue. This is the same reason that you can't do this:
int function(int& x) {
x++;
}
int main() {
function(137); // Error - 137 is an rvalue, but lvalue is needed.
}
Hope this helps!

error while working with boost::sregex_token_iterator

I want to search for a regular expression and print it with a color. I used boost::sregex_token_iterator to do this. this is my code
boost::regex re("ab.");
string s="";
string buf;
string infile("retest.txt");
//string color="green";
ifstream in(infile.c_str());
int lcount=0;
while (getline(in,buf))
{
boost::sregex_token_iterator p(buf.begin(), buf.end(), re, 0);
boost::sregex_token_iterator end;
lcount++;
cout <<"line : "<<lcount<<endl;
for (;p != end;++p)
{
string m(p->first, p->second);
cout<< m <<endl;
//cout <<*(p->first)<<endl;
//cout <<*(p->second)<<endl;
//unsigned int pos = buf.find(m);
buf = buf.insert(p->first,"\e[0;32m");
buf = buf.insert(p->second+m.length()+7,"\e[0m");
}
cout<<"\n";
s.append(buf);
s.append("\n");
}
in.close();
cout <<"s is: "<<s<<endl;
return 0;
}
but I get this error :
In file included from /usr/include/boost/config.hpp:35:0,
from /usr/include/boost/regex/config.hpp:53,
from /usr/include/boost/regex.hpp:28,
from main3.cc:3:
/usr/include/boost/config/compiler/gcc.hpp:92:7: warning: #warning "Unknown compiler version - please run the configure tests and report the results" [-Wcpp]
main3.cc: In function âint main(int, char**)â:
main3.cc:36:40: error: no matching function for call to âstd::basic_string::insert(const __gnu_cxx::__normal_iterator >&, const char [8])â
main3.cc:36:40: note: candidates are:
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:54:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from main3.cc:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1182:7: note: void std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, std::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator >; typename _Alloc::rebind<_CharT>::other::pointer = char*; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1182:7: note: candidate expects 3 arguments, 2 provided
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1199:9: note: template void std::basic_string::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, _InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1199:9: note: template argument deduction/substitution failed:
main3.cc:36:40: note: cannot convert âp.boost::regex_token_iterator::operator-><__gnu_cxx::__normal_iterator >, char, boost::regex_traits, std::allocator >()->boost::sub_match<__gnu_cxx::__normal_iterator > >::.std::pair<__gnu_cxx::__normal_iterator >, __gnu_cxx::__normal_iterator > >::firstâ (type âconst __gnu_cxx::__normal_iterator >â) to type âstd::basic_string::iterator {aka __gnu_cxx::__normal_iterator >}â
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:54:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from main3.cc:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1230:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1230:7: note: no known conversion for argument 1 from âconst __gnu_cxx::__normal_iterator >â to âstd::basic_string::size_type {aka unsigned int}â
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1252:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const std::basic_string<_CharT, _Traits, _Alloc>&, std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1252:7: note: candidate expects 4 arguments, 2 provided
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:55:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from main3.cc:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.tcc:361:6: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.tcc:361:6: note: candidate expects 3 arguments, 2 provided
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:54:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from main3.cc:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1293:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _CharT*) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1293:7: note: no known conversion for argument 1 from âconst __gnu_cxx::__normal_iterator >â to âstd::basic_string::size_type {aka unsigned int}â
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1316:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1316:7: note: candidate expects 3 arguments, 2 provided
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1334:7: note: std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, _CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator >; typename _Alloc::rebind<_CharT>::other::pointer = char*]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1334:7: note: no known conversion for argument 1 from âconst __gnu_cxx::__normal_iterator >â to âstd::basic_string::iterator {aka __gnu_cxx::__normal_iterator >}â
main3.cc:37:51: error: no matching function for call to âstd::basic_string::insert(__gnu_cxx::__normal_iterator >, const char [5])â
main3.cc:37:51: note: candidates are:
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:54:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from main3.cc:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1182:7: note: void std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, std::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator >; typename _Alloc::rebind<_CharT>::other::pointer = char*; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1182:7: note: candidate expects 3 arguments, 2 provided
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1199:9: note: template void std::basic_string::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, _InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1199:9: note: template argument deduction/substitution failed:
main3.cc:37:51: note: cannot convert âp.boost::regex_token_iterator::operator-><__gnu_cxx::__normal_iterator >, char, boost::regex_traits, std::allocator >()->boost::sub_match<__gnu_cxx::__normal_iterator > >::.std::pair<__gnu_cxx::__normal_iterator >, __gnu_cxx::__normal_iterator > >::second.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator+ >((* &((__gnu_cxx::__normal_iterator >::difference_type)m.std::basic_string<_CharT, _Traits, Alloc>::length, std::allocator >())))._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator+ >((* &7))â (type â__gnu_cxx::__normal_iterator >â) to type âstd::basic_string::iterator {aka __gnu_cxx::__normal_iterator >}â
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:54:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from main3.cc:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1230:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1230:7: note: no known conversion for argument 1 from â__gnu_cxx::__normal_iterator >â to âstd::basic_string::size_type {aka unsigned int}â
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1252:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const std::basic_string<_CharT, _Traits, _Alloc>&, std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1252:7: note: candidate expects 4 arguments, 2 provided
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:55:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from main3.cc:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.tcc:361:6: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.tcc:361:6: note: candidate expects 3 arguments, 2 provided
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:54:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from main3.cc:1:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1293:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _CharT*) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1293:7: note: no known conversion for argument 1 from â__gnu_cxx::__normal_iterator >â to âstd::basic_string::size_type {aka unsigned int}â
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1316:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string; std::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1316:7: note: candidate expects 3 arguments, 2 provided
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1334:7: note: std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, _CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator >; typename _Alloc::rebind<_CharT>::other::pointer = char*]
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:1334:7: note: no known conversion for argument 1 from â__gnu_cxx::__normal_iterator >â to âstd::basic_string::iterator {aka __gnu_cxx::__normal_iterator >}â
I know that the type of p->first is not the one which insert function accepts. but I don't know how to convert these to each other.
The issue might be your usage of std::string::insert. I can't see an insert(iterator, string) version among it's numerous overloads.
You might want to call desired version by explicitly stating length of char array:
void insert (iterator p, size_t n, char c)
The problem seems to be that boost is configured for use with a different version of the compiler rather than the one you are currently using.
Could you add the details of how you compiled it as well?

gcc linker errors when using boost to_lower & trim

I'm trying to use the boost library in my code but get the following linker errors under Sparc Solaris platform.
The problem code can essentially be summarised to:
#include <boost/algorithm/string.hpp>
std::string xparam;
...
xparam = boost::to_lower(xparam);
The linker error is:
LdapClient.cc:349: no match for `std::string& = void' operator
/opt/gcc-3.2.3/include/c++/3.2.3/bits/basic_string.h:338: candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/opt/gcc-3.2.3/include/c++/3.2.3/bits/basic_string.h:341: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/opt/gcc-3.2.3/include/c++/3.2.3/bits/basic_string.h:344: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
gmake: *** [LdapClient.o] Error 1
Any ideas?
boost::to_lower does not return a copy of the string, it operates on the variable passed into the function. For some examples, read this.
So no need to reassign:
boost::to_lower(xparam);
You will get an error because you are trying to assign the string to the value void.
If you want to make a copy of it, use the copy version:
std::string xparamLowered = boost::to_lower_copy(xparam);
boost::to_lower modifies the string in-place, it does not return a new string. This is sufficient:
boost::to_lower(xparam);
And your code doesn't compile because the return type of to_lower is void (as the error message says).