473,487 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

const and non-const string iterators

Hi all. G++ fails to compile the following:

#include <string>

int main()
{
std::string foo("abc=123");
std::string::const_iterator delimiter
= std::find(foo.begin(), foo.end(), '=');

std::string left (foo.begin(), delimiter);
}

The construction of 'left' fails, saying that there's no matching
constructor. The problem appears to be that it is searching
for a constructor taking (iterator, const_iterator) and finds
none. Is this conforming behaviour?

The following change doesn't fix it:

std::string::const_iterator
begin = foo.begin(),
delimiter = std::find(begin, foo.end(), '=');
std::string left (begin, delimiter);

Now it complains that there's no match for std::find() for
the same reason. The following does compile correctly:

std::string::const_iterator
begin = foo.begin(),
end = foo.end(),
delimiter = std::find(begin, end, '=');
std::string left (begin, delimiter);

I thought that non-const iterators should be implicitly
converted to const ones, or that the version of begin()
that returns a const_iterator should be found when it's
trying to find a matching call to std::find().
BCC has no problem with the code.

Jul 23 '05 #1
3 2453

"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi all. G++ fails to compile the following:

#include <string>

int main()
{
std::string foo("abc=123");
std::string::const_iterator delimiter
= std::find(foo.begin(), foo.end(), '=');

std::string left (foo.begin(), delimiter);
}

The construction of 'left' fails, saying that there's no matching
constructor. The problem appears to be that it is searching
for a constructor taking (iterator, const_iterator) and finds
none. Is this conforming behaviour?
Yes.
The function template you are trying to instanciate has the following
declaration:

template <class InputIterator >
basic_string(
InputIterator _First,
InputIterator _Last,
const allocator_type& _Al = Allocator ( )
);

You can see that the type of the first and second parameters of the function
template correspond to the same type. The compiler cannot deduce that it is
this constructor that you want to call if your call has a combination of
string::const_iterator and string::iterator as arguments.

The following change doesn't fix it:

std::string::const_iterator
begin = foo.begin(),
delimiter = std::find(begin, foo.end(), '=');
std::string left (begin, delimiter);

Now it complains that there's no match for std::find() for
the same reason.
Same problem here. The general find function is declared as:

template<class InputIterator, class Type>
InputIterator find(
InputIterator _First,
InputIterator _Last,
const Type& _Val
);

Again _First and _Last must have the same type.

The following does compile correctly:

std::string::const_iterator
begin = foo.begin(),
end = foo.end(),
delimiter = std::find(begin, end, '=');
std::string left (begin, delimiter);

I thought that non-const iterators should be implicitly
converted to const ones, or that the version of begin()
that returns a const_iterator should be found when it's
trying to find a matching call to std::find().
BCC has no problem with the code.

There is no type conversion for the instanciation of function templates. A
template type parameter (e.g. InputIterator) is substituted with the same
type (e.g. string::const_iterator) wherever it appears in the template
definition to generate the template instance.
Regards,

Thierry Miceli
www.ideat-solutions.com

Jul 23 '05 #2
Well, I compiled your code in VC++ 7.1 and it compiled and worked correctly:

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
string foo = "abc=123";
string::const_iterator delimiter = std::find(foo.begin(), foo.end(),
'=');
string left(foo.begin(), delimiter);

cout << left << endl;
return 0;
}

Is that a recent version of g++ that you tested it on? I guess VC++ 7.1 just
looks harder for constructors :)
The following change doesn't fix it:

std::string::const_iterator
begin = foo.begin(),
delimiter = std::find(begin, foo.end(), '=');
std::string left (begin, delimiter);
Well, if you want to use that, change:
delimiter = std::find(begin, foo.end(), '='); to: delimiter = std::find(foo.begin(), foo.end(), '=');
Hope that helps,
George Faraj

"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com... Hi all. G++ fails to compile the following:

#include <string>

int main()
{
std::string foo("abc=123");
std::string::const_iterator delimiter
= std::find(foo.begin(), foo.end(), '=');

std::string left (foo.begin(), delimiter);
}

The construction of 'left' fails, saying that there's no matching
constructor. The problem appears to be that it is searching
for a constructor taking (iterator, const_iterator) and finds
none. Is this conforming behaviour?

The following change doesn't fix it:

std::string::const_iterator
begin = foo.begin(),
delimiter = std::find(begin, foo.end(), '=');
std::string left (begin, delimiter);

Now it complains that there's no match for std::find() for
the same reason. The following does compile correctly:

std::string::const_iterator
begin = foo.begin(),
end = foo.end(),
delimiter = std::find(begin, end, '=');
std::string left (begin, delimiter);

I thought that non-const iterators should be implicitly
converted to const ones, or that the version of begin()
that returns a const_iterator should be found when it's
trying to find a matching call to std::find().
BCC has no problem with the code.

Jul 23 '05 #3

"George Faraj" <gi*****@hotmail.com> wrote in message
news:d3**********@domitilla.aioe.org...
Well, I compiled your code in VC++ 7.1 and it compiled and worked

correctly:
It does not compile with Comeau which is one of the best compilers (if not
the best) at implementing the C++ standard.
I get the following error:

line 12: error: no instance of constructor

This code should not compile since string::iterator and
string::const_iterator are different types.
Anybody as an opinion on this? Is VC++ wrong?
Thierry Miceli
www.ideat-solutions.com
Jul 23 '05 #4

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

Similar topics

12
4395
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
3
12184
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
7537
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
4464
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
8
3480
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
14
8325
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
11
3393
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
6087
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
2316
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
12626
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
0
7132
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,...
1
6846
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
7341
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
5439
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
4870
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
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
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 ...
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
266
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.