PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > std::tr1::array iterator types
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
std::tr1::array iterator types

Réponse
 
LinkBack Outils de la discussion
Vieux 07/04/2008, 09h53   #1
Fokko Beekhof
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut std::tr1::array iterator types

Hello,

The following code compiles (gcc/linux/gnu platform)
//////////////////////
#include <tr1/array>

int main()
{
std::tr1::array<int, 7> a;
std::tr1::array<int, 6>::iterator i = a.begin()+1;

return 0;
}
/////////////////////

That it compiles is "logical" if you consider that what remains after
the first element of a an array of length 7, is an array of length 6.

It is also "illogical" if you consider that the iterator returned by
a.begin() is of type std::tr1::array<int, 7>::iterator, and merely
adding 1 to it should cause it to change its type.

Unless, of course, all iterators of type
std::tr1::array<int, X>::iterator are the same for all X.

Question: is it indeed guaranteed in the standard that all types
std::tr1::array<int, X>::iterator are identical for all X ?

Thanks in advance,
Fokko Beekhof
  Réponse avec citation
Vieux 07/04/2008, 18h00   #2
Bo Persson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: std::tr1::array iterator types

Fokko Beekhof wrote:
> Hello,
>
> The following code compiles (gcc/linux/gnu platform)
> //////////////////////
> #include <tr1/array>
>
> int main()
> {
> std::tr1::array<int, 7> a;
> std::tr1::array<int, 6>::iterator i = a.begin()+1;
>
> return 0;
> }
> /////////////////////
>
> That it compiles is "logical" if you consider that what remains
> after the first element of a an array of length 7, is an array of
> length 6.
>
> It is also "illogical" if you consider that the iterator returned by
> a.begin() is of type std::tr1::array<int, 7>::iterator, and merely
> adding 1 to it should cause it to change its type.
>
> Unless, of course, all iterators of type
> std::tr1::array<int, X>::iterator are the same for all X.
>
> Question: is it indeed guaranteed in the standard that all types
> std::tr1::array<int, X>::iterator are identical for all X ?
>


No, there are no such guarantees.

That it happens to work is probably an effect of the requirements for
iterator::value_type, which of course must be int for all
array<int,X>.


Bo Persson


  Réponse avec citation
Vieux 07/04/2008, 18h17   #3
Pete Becker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: std::tr1::array iterator types

On 2008-04-07 13:00:57 -0400, "Bo Persson" <bop@gmb.dk> said:

> Fokko Beekhof wrote:
>> Hello,
>>
>> The following code compiles (gcc/linux/gnu platform)
>> //////////////////////
>> #include <tr1/array>
>>
>> int main()
>> {
>> std::tr1::array<int, 7> a;
>> std::tr1::array<int, 6>::iterator i = a.begin()+1;
>>
>> return 0;
>> }
>> /////////////////////
>>
>> That it compiles is "logical" if you consider that what remains
>> after the first element of a an array of length 7, is an array of
>> length 6.
>>
>> It is also "illogical" if you consider that the iterator returned by
>> a.begin() is of type std::tr1::array<int, 7>::iterator, and merely
>> adding 1 to it should cause it to change its type.
>>
>> Unless, of course, all iterators of type
>> std::tr1::array<int, X>::iterator are the same for all X.
>>
>> Question: is it indeed guaranteed in the standard that all types
>> std::tr1::array<int, X>::iterator are identical for all X ?
>>

>
> No, there are no such guarantees.
>
> That it happens to work is probably an effect of the requirements for
> iterator::value_type, which of course must be int for all
> array<int,X>.
>
>


And, in particular, it's probably because that implementation uses T*
as its iterator type. If it used a nested class (to do bounds checking,
for example), then array<int,6>::iterator and array<int,7>::iterator
would be two different types.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h47.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09670 seconds with 11 queries