Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > correct idiom for container

Reply
Thread Tools

correct idiom for container

 
 
keith
Guest
Posts: n/a
 
      04-08-2010
Is the following code well-formed and portable?

Value value;
Container<Value> c;

c.push_back(value);
c.erase(--c.end());

Container can be std::string, std::vector...
 
Reply With Quote
 
 
 
 
Jonathan Lee
Guest
Posts: n/a
 
      04-08-2010
On Apr 8, 11:46*am, Pete Becker <p...@versatilecoding.com> wrote:
> It's not quite that simple. If the iterator is a class type you can
> decrement it. So if the iterator isn't a class type, the code is
> ill-formed; if the iterator is a class type, the code works fine. But
> it's not portable.


Using rbegin() instead of --end() should fix that.

--Jonathan
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      04-08-2010
On Apr 8, 4:46 pm, Pete Becker <p...@versatilecoding.com> wrote:
> Victor Bazarov wrote:
> > keith wrote:
> >> Is the following code well-formed and portable?


> >> Value value;
> >> Container<Value> c;


> >> c.push_back(value);
> >> c.erase(--c.end());


> >> Container can be std::string, std::vector...


> > First thing that came to my mind: c.end() returns a
> > *temporary* which isn't an lvalue. Decrement requires an
> > lvalue, hence the code that has (--c.end()) is ill-formed.
> > Ill-formed program cannot be portable. So, the answer to
> > your question is "No".


> It's not quite that simple. If the iterator is a class type you can
> decrement it.


Maybe. If the iterator is a class type and the increment
operator is a member, you can decrement it. If the increment
operator is a free function, taking the iterator as a non-const
reference, you can't. (I don't know of any implementations
which do it that way, but I don't know why. It's the way I'd do
it.)

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
container idiom revisited keith C++ 9 04-15-2010 08:10 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
std::transform container => std::abs(container) Steven T. Hatton C++ 4 12-05-2004 07:10 AM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57