This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: multiset forward iterator is const



Hi Benjamin.  Your code compiles fine for me as well.  However, in
your code, 'y' is a const reference (const T&), not a reference (T&)
as required by the spec.  You can demonstrate this by compiling (or
failing to compile) the code:

#include <set>

int main(void)
{
  typedef  multiset<int, less<int> > multiset_type;
  multiset_type x;
  x.insert(5);
  multiset_type::iterator iter = x.begin();
  multiset_type::reference y = *iter;
  y = 6;
}

[patricia:~]g++ -o test test.cpp
test.cpp: In function `int main()':
test.cpp:10: assignment of read-only reference `y'
[patricia:~]

To be more clear, multiset_type::reference and int& must be the same
type and they are not.

David Marwood


Benjamin Kosnik writes:
: > International Standard for Information Systems Programming Language
: > C++" says that the multiset must support forward iterators.  A forward
: > iterator's operator* must return a reference (T&).  The implementation
: 
: This code, which differs from yours, compiles without warning with the 
: v-3 release (which is not the same as 2.90, and is the expressed subject 
: of  this list.)
: 
: #include <set>
: 
: int main(void)
: {
:   typedef  multiset<int, less<int> > multiset_type;
:   multiset_type x;
:   x.insert(5);
:   multiset_type::iterator iter = x.begin();
:   multiset_type::reference y = *iter;
: }

Matthias Klose writes:
: "The Dec 2, 1996 draft of the "Working Paper for Draft Proposed
: International Standard for Information Systems Programming Language
: C++" says that the multiset must support forward iterators.  A forward
: iterator's operator* must return a reference (T&).  The implementation
: in libstdc++2.9-dev multiset.h returns a const reference (const T&).
: This is not correct."
: 
: #include <multiset.h>
: 
: int main(void)
: {
:   multiset<int, less<int> > x;
:   x.insert(5);
:   multiset<int, less<int> >::iterator iter = x.begin();
:   int& y = *iter;
: }
: [patricia:~]g++ test.cpp
: test.cpp: In function `int main(...)':
: test.cpp:8: warning: conversion from `const int' to `int &' discards const

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]