Hi, i.e. code like list<int> bla; // insert some stuff into bla list<int>::iterator it(bla.begin()); advance(it, bla.size() * 0.5); can lead to an endless loop. I know that the code is "broken" but it should work nevertheless. Please change the code to < 0 and > 0, not just while (n--). Cheers, André
What does the standard says about this case?
Subject: Re: std::advance() isn't stable for floating point numbers On Monday 30 January 2006 00:32, pinskia at gcc dot gnu dot org wrote: > ------- Comment #1 from pinskia at gcc dot gnu dot org 2006-01-29 > 23:32 ------- > What does the standard says about this case? Don't know, but I was suprised that Distance is a template parameter.
AFAICS, the standard is silent about this issue. I think the suggested change is correct.
Subject: Re: std::advance() isn't stable for floating point numbers "pcarlini at suse dot de" <gcc-bugzilla@gcc.gnu.org> writes: | AFAICS, the standard is silent about this issue. I think the suggested change | is correct. I believe Martin Sebor filled a related PR, whereby we should have converted n to *some* integral type before proceeding... -- Gaby
(In reply to comment #4) > | AFAICS, the standard is silent about this issue. I think the suggested > | change is correct. > > I believe Martin Sebor filled a related PR, whereby we should have > converted n to *some* integral type before proceeding... I was also under that impression. That issue, however, was about Size in algos, which, at least, is required to be convertible to an integral type. I don't think we even have such *minimal* requirement here... What do you suggest? Should the LWG discuss such kind of requirements for Distance too? In the meanwhile suspend? I have no strong opinions...
Hi again. On second tought, I don't think we have an issue here. Maybe of QoI only, at most. The reason is that, according to 24.1/9, Distance is a difference type in the "following sections". A bit vague, but that seem to imply to also the template argument Distance in 24.3.4 is a difference type...
Subject: Re: std::advance() isn't stable for floating point numbers "pcarlini at suse dot de" <gcc-bugzilla@gcc.gnu.org> writes: | ------- Comment #5 from pcarlini at suse dot de 2006-01-30 00:45 ------- | (In reply to comment #4) | > | AFAICS, the standard is silent about this issue. I think the suggested | > | change is correct. | > | > I believe Martin Sebor filled a related PR, whereby we should have | > converted n to *some* integral type before proceeding... | | I was also under that impression. That issue, however, was about Size in algos, sorry; memory failing. | which, at least, is required to be convertible to an integral type. I don't | think we even have such *minimal* requirement here... What do you suggest? reading the standard specification let me under the impression that Distance is supposed to "related" to difference_type. For example the distance between the p before and after calling distance(p, n) is supposed to be n. So I would suggest conversion to the difference_type of the iterator as a momentary resolution. | Should the LWG discuss such kind of requirements for Distance too? Definitely. | In the meanwhile suspend? I have no strong opinions... I'm not sure the resolution would introduce an ABI breakage, so I would say let's do both; but I'm open to other alternatives. -- Gaby
Subject: Re: std::advance() isn't stable for floating point numbers "pcarlini at suse dot de" <gcc-bugzilla@gcc.gnu.org> writes: | Hi again. On second tought, I don't think we have an issue here. Maybe of QoI | only, at most. The reason is that, according to 24.1/9, Distance is a | difference type in the "following sections". A bit vague, but that seem to | imply to also the template argument Distance in 24.3.4 is a difference type... That matches my interpretation --- hence Distance must behave "like" an integral type -- but still, the question is exactly what kind of relation does it have with say std::iterator<FowardIterator>::difference_type. -- Gaby
(In reply to comment #7) > reading the standard specification let me under the impression that > Distance is supposed to "related" to difference_type. For example > the distance between the p before and after calling distance(p, n) is > supposed to be n. So I would suggest conversion to the > difference_type of the iterator as a momentary resolution. Excellent idea. > I'm not sure the resolution would introduce an ABI breakage, so I > would say let's do both; but I'm open to other alternatives. Agreed, let's implement your suggestion and send a note to the reflector (and Martin, already working on the Size issue). Thanks!
Subject: Re: std::advance() isn't stable for floating point numbers On Monday 30 January 2006 02:09, pcarlini at suse dot de wrote: > ------- Comment #9 from pcarlini at suse dot de 2006-01-30 01:09 > ------- (In reply to comment #7) > > > reading the standard specification let me under the impression that > > Distance is supposed to "related" to difference_type. For example > > the distance between the p before and after calling distance(p, n) > > is supposed to be n. So I would suggest conversion to the > > difference_type of the iterator as a momentary resolution. > > Excellent idea. FYI, I suggested tests for > 0 and < 0 as MSVC 7.1 does it that way. If you convert Distance to difference_type the question is what happens if Distance is i.e. 0.2. Do you advance by 0 or 1 position?
Subject: Re: std::advance() isn't stable for floating point numbers "Woebbeking at web dot de" <gcc-bugzilla@gcc.gnu.org> writes: | On Monday 30 January 2006 02:09, pcarlini at suse dot de wrote: | > ------- Comment #9 from pcarlini at suse dot de 2006-01-30 01:09 | > ------- (In reply to comment #7) | > | > > reading the standard specification let me under the impression that | > > Distance is supposed to "related" to difference_type. For example | > > the distance between the p before and after calling distance(p, n) | > > is supposed to be n. So I would suggest conversion to the | > > difference_type of the iterator as a momentary resolution. | > | > Excellent idea. | | FYI, I suggested tests for > 0 and < 0 as MSVC 7.1 does it that way. | | If you convert Distance to difference_type the question is what happens | if Distance is i.e. 0.2. Do you advance by 0 or 1 position? does not that follow from the conversion rule? -- Gaby
Subject: Re: std::advance() isn't stable for floating point numbers On Monday 30 January 2006 08:54, gdr at integrable-solutions dot net wrote: > ------- Comment #11 from gdr at integrable-solutions dot net > | FYI, I suggested tests for > 0 and < 0 as MSVC 7.1 does it that > | way. > | > | If you convert Distance to difference_type the question is what > | happens if Distance is i.e. 0.2. Do you advance by 0 or 1 position? > > does not that follow from the conversion rule? Of course. I meant what should it do. Do you want it to advance by 0 or 1 position? Do we just use a simple cast or do we need to do some rounding? AFAIK MSVC advance by 1 in this case.
Subject: Re: std::advance() isn't stable for floating point numbers "Woebbeking at web dot de" <gcc-bugzilla@gcc.gnu.org> writes: | Subject: Re: std::advance() isn't stable for floating point numbers | | On Monday 30 January 2006 08:54, gdr at integrable-solutions dot net | wrote: | > ------- Comment #11 from gdr at integrable-solutions dot net | | > | FYI, I suggested tests for > 0 and < 0 as MSVC 7.1 does it that | > | way. | > | | > | If you convert Distance to difference_type the question is what | > | happens if Distance is i.e. 0.2. Do you advance by 0 or 1 position? | > | > does not that follow from the conversion rule? | | Of course. I meant what should it do. Do you want it to advance by 0 or | 1 position? Do we just use a simple cast or do we need to do some | rounding? AFAIK MSVC advance by 1 in this case. Given that Distance is supposed to behave "like" en integral type in the first place, I would simply use: (1) implicit conversion to convert it to difference_type; (2) advance the number of time computed from (1). -- Gaby
Subject: Bug 26020 Author: paolo Date: Tue Oct 17 15:17:32 2006 New Revision: 117827 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=117827 Log: 2006-10-17 Paolo Carlini <pcarlini@suse.de> PR libstdc++/26020 * include/bits/stl_iterator_base_funcs.h (advance): Convert distance parameter to iterator_traits<>::difference_type. * testsuite/24_iterators/26020.cc: New. Added: trunk/libstdc++-v3/testsuite/24_iterators/26020.cc Modified: trunk/libstdc++-v3/ChangeLog trunk/libstdc++-v3/include/bits/stl_iterator_base_funcs.h
Fixed for 4.2.0.