Topic: Where next for Standard C++? Type-coerced assign?
Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1997/11/30 Raw View
Andrew Fitzgibbon wrote:
>
> I'd like a type-coercing assignment operator
> something like
>
> for(class p = cont.begin(); ...
>
> which automagically makes p's type agree with the expression on the rhs.
Interesting, but it would really be a type-coercing initializer, not assignment
operator. I'm not sure I like using the keyword "class", because although it
probably doesn't introduce any ambiguities, the mechanism should work for
non-class types, too:
for (type i = 0; i < 100; ++i) ...
> Alternative syntaxes might be (assuming implicit int is removed)
>
> for(auto p = cont.begin(); ...
There is already a meaning for auto in the language.
> or
>
> for(p := cont.begin(); ...
I'd avoid adding any new tokens.
--
Ciao,
Paul
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Andrew Fitzgibbon <awf@robots.ox.ac.uk>
Date: 1997/11/30 Raw View
fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson) writes:
> I suppose in the mean time you could use
>
> #define DECLARE(var, exprn) typeof(exprn) var = (exprn)
> ...
> for (DECLARE(p, cont.begin()); ...
>
> but of course this has the usual disadvantages of macros.
I used to, but it had the usual disadvantages of macros :)
Also, of course, no "other" compiler accepted it.
--
Andrew Fitzgibbon, awf@robots.ox.ac.uk
Oxford Information Engineering Research Group +44 01865 273127
<a href=http://www.robots.ox.ac.uk/~awf> Home Page </a>
"Never say there is no way"
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: jbuck@synopsys.com (Joe Buck)
Date: 1997/11/30 Raw View
Andrew Fitzgibbon writes:
> I'd like a type-coercing assignment operator
>something like
>
> for(class p = cont.begin(); ...
Fergus Henderson writes:
> John Skaller has previously suggested this, with the syntax
> for (declare p = cont.begin(); ...
> where "declare" is a new keyword.
Doesn't work, as there are two begin() methods for standard containers,
one returning an iterator, one returning a const_iterator. Seems we
can't really improve on
for (typeof(cont)::iterator p = cont.begin(); ...
or
for (typeof(cont)::const_iterator p = cont.begin(); ...
unless we permitted something like
for (cont::iterator p = cont.begin(); ...
--
-- Joe Buck http://www.synopsys.com/pubs/research/people/jbuck.html
If you thought flashing ads on Web sites were annoying, wait till the Web
and your operating system are "seamlessly integrated" and watch the
pulsating promotional crud spill out over your desktop. -- Scott Rosenburg
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Andrew Fitzgibbon <awf@robots.ox.ac.uk>
Date: 1997/11/29 Raw View
I've always thought it would be nice not to have to change code if a
container type changes. Currently if I cnage my container from vector to
list, I must change
for(vector<int>::iterator p = cont.begin(); p != cont.end(); ++p)
to
for(list<int>::iterator p = cont.begin(); p != cont.end(); ++p)
With typeof, I could use
for(typeof(cont.begin()) p = cont.begin(); p != cont.end(); ++p)
but it's a bit messy as I have to repeat the code fragment "cont.begin()"
to ensure the types match. I'd like a type-coercing assignment operator
something like
for(class p = cont.begin(); ...
which automagically makes p's type agree with the expression on the rhs.
Alternative syntaxes might be (assuming implicit int is removed)
for(auto p = cont.begin(); ...
or
for(p := cont.begin(); ...
--
Andrew Fitzgibbon, awf@robots.ox.ac.uk
Oxford Information Engineering Research Group +44 01865 273127
<a href=http://www.robots.ox.ac.uk/~awf> Home Page </a>
"Never say there is no way"
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/11/29 Raw View
Andrew Fitzgibbon <awf@robots.ox.ac.uk> writes:
>With typeof, I could use
>
> for(typeof(cont.begin()) p = cont.begin(); p != cont.end(); ++p)
>
>but it's a bit messy as I have to repeat the code fragment "cont.begin()"
>to ensure the types match. I'd like a type-coercing assignment operator
>something like
>
> for(class p = cont.begin(); ...
John Skaller has previously suggested this, with the syntax
for (declare p = cont.begin(); ...
where "declare" is a new keyword.
I suppose in the mean time you could use
#define DECLARE(var, exprn) typeof(exprn) var = (exprn)
...
for (DECLARE(p, cont.begin()); ...
but of course this has the usual disadvantages of macros.
--
Fergus Henderson <fjh@cs.mu.oz.au> WWW: <http://www.cs.mu.oz.au/~fjh>
Note: due to some buggy software and a (probably accidental)
denial-of-service attack, any mail sent to me or comp.std.c++ between
Tue Nov 25 20:00:00 UTC (6am Wed, local time)
and Wed Nov 26 06:00:00 UTC (4pm, local time)
may have gone into the bit-bucket. Please re-send it.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Pete Becker <petebecker@acm.org>
Date: 1997/11/29 Raw View
Andrew Fitzgibbon wrote:
>
> I've always thought it would be nice not to have to change code if a
> container type changes. Currently if I cnage my container from vector to
> list, I must change
>
> for(vector<int>::iterator p = cont.begin(); p != cont.end(); ++p)
>
> to
>
> for(list<int>::iterator p = cont.begin(); p != cont.end(); ++p)
>
> With typeof, I could use
>
With typedef you can do it today, without needing any extensions:
typedef vector<int> container;
typedef container::iterator iterator;
for( iterator p = cont.begin(); p != cont.end(); ++p )
On the other hand, this use of a for loop is not at all idiomatic for
STL, so it would probably be better to use STL in the way that it was
designed to be used:
template<class T> void whatever( T begin, T end )
{
// omitted code goes here
}
call it with:
whatever( cont.begin(), cont.end() );
In general, if you're creating named iterators you're probably doing
something wrong.
-- Pete
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]