Prev: const is an overrated concept that is a source of extra typing and maintenan
Next: performance of map in MSVC 10b2
From: Ulrich Eckhardt on 7 Apr 2010 18:32 Herb Sutter wrote: >>> > Is there some standard/boost equivalent to a function like: >>> > template< typename T > >>> > T successor( T r ) >>> > { >>> > return( ++r ); > > While others may be inclined to give boring and pedestrian answers > like "boost::next(r)", I'm just bleary enough from being in all-day > meetings to suggest that the current standard spelling for "successor" > is already "++T"... :-) (Just add "(r)" after each. See?) > > I'm not being serious, I like next(r) and type deduction as much as > the next guy. Here's one cake for you then, to both eat and keep: template<typename T> T copy(T const& t) { return t; } iterator i0 = ...; iterator i1 = ++copy(i0); :) Uli -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 7 Apr 2010 18:31 On 8 Apr., 02:59, Herb Sutter <herb.sut...(a)gmail.com> wrote: > >> > Is there some standard/boost equivalent to a function like: > >> > template< typename T > > >> > T successor( T r ) > >> > { > >> > return( ++r ); > > While others may be inclined to give boring and pedestrian answers > like "boost::next(r)", I'm just bleary enough from being in all-day > meetings to suggest that the current standard spelling for "successor" > is already "++T"... :-) (Just add "(r)" after each. See?) There are some use-cases where next/prev allow more general expressions. Consider a function that returns an iterator that is guaranteed to be incrementable, you may want to write // Precondition: !c.empty() template<class Container> void foo(Container& c) { typename Container::iterator it = ++c.begin(); ... } but this code is not guaranteed to be well-formed, because Container::iterator might be a normal pointer which can only invoke the pre-increment operator on lvalues. But with std::next available, we can just write // Precondition: !c.empty() template<class Container> void foo(Container& c) { typename Container::iterator it = std::next(c.begin()); ... } Greetings from Bremen, Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: itaj sherman on 8 Apr 2010 02:18 On Apr 8, 12:32 pm, Ulrich Eckhardt <eckha...(a)satorlaser.com> wrote: > > Here's one cake for you then, to both eat and keep: > > template<typename T> > T copy(T const& t) > { return t; } > > iterator i0 = ...; > iterator i1 = ++copy(i0); > > :) > > Uli > I'm not sure I get this: how does the temporary returned by copy bind to a reference parameter of operator++. Is it some rvalue-reference version of operator++? Can it be done in c++03? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 8 Apr 2010 07:59 On 8 Apr., 19:18, itaj sherman <itajsher...(a)gmail.com> wrote: > On Apr 8, 12:32 pm, Ulrich Eckhardt <eckha...(a)satorlaser.com> wrote: > > > Here's one cake for you then, to both eat and keep: > > > template<typename T> > > T copy(T const& t) > > { return t; } > > > iterator i0 = ...; > > iterator i1 = ++copy(i0); > > > :) > > > Uli > > I'm not sure I get this: how does the temporary returned by copy bind > to a reference parameter of operator++. Is it some rvalue-reference > version of operator++? Can it be done in c++03? There is no reference binding intended. I think that Uli just wants to demonstrate that above code is not guaranteed to be well-formed: For built-in pointers an lvalue is required for the preincrement operator, but copy returns an rvalue. HTH & Greetings from Bremen, Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: itaj sherman on 8 Apr 2010 08:00 On Apr 7, 11:57 pm, Francis Glassborow <francis.glassbo...(a)btinternet.com> wrote: > itaj sherman wrote: > > Is there some standard/boost equivalent to a function like: > > > template< typename T > > > T successor( T r ) > > { > > return( ++r ); > > why not > return r+1; > As noted in other replies, r+1 is not the same as the return value of + +r. It might not even be defined for some concepts, and for others the performance would probably be different. I could even imagine types for which it would have a different value, but many don't like it. Nevertheless, I would expect any defined operator++ to have the side- effect of changing the parameter to the same value it returns. My question is triggered by that ++r is not a functional expression (it has side-effects), and I wanted to know if there was a parallel functional expression that is compact and commonly used for it. I expected there might be a function to supply this in boost/std. If you had asked: "why not return (r+=1)", I would have answered that unlike ++r, (r+=1) does already have a functional form which is compact and commonly used, namely r+1. So wrapping r+=1 or r+1 in a function is not needed for that reason as ++r, and I wouldn't expect std/boost to have it. > > } > > > That can be used when the return value of operator++() is needed but > > not the side effect (notice non reference parameter). > itaj -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: const is an overrated concept that is a source of extra typing and maintenan Next: performance of map in MSVC 10b2 |