NO, please. Just use std::cref(). The feature is there already in the STL.
Am 19.02.2015 um 20:36 schrieb Thiago Macieira: > On Thursday 19 February 2015 12:07:04 Matthew Woehlke wrote: >> On 2015-02-19 07:29, Daniel Teske wrote: >>> Qt's container classes and C++11 range based for loop do not mix very >>> well. >>> Ranged based for uses std::begin(container), which if not overloaded calls >>> container.begin(), which detaches. >> >> As an aside, the "correct" fix for this IMHO is for range-based for to >> support a mechanism for marking the RHS 'const', whether or not it >> otherwise would be so. >> >> Worst case, Qt could (should?) implement something like: >> >> struct QConstWrapper<ContainerType> >> { >> ContainerType::const_iterator begin() const; >> ContainerType::const_iterator end() const; >> // remaining "magic" elided >> }; >> >> QConstWrapper<ContainerType> qConst(ContainerType const&); >> >> That said, note that range-based for also differs from foreach in that >> the former operates on the original container, whereas the latter >> operates on a copy. > > It actually needs to be: > > template <typename T> const T qConst(const T &t) { return t; } > > It needs to create a copy and return it. There's a gotcha with range-based for > that it is defined in such a way that your temporaries may be destroyed before > the iteration. The standard defines it as: > > { > auto && __range = <your range>; > for ( auto __begin = std::begin(__range), > __end = std::end(__range); > __begin != __end; > ++__begin ) { > <for-range-declaration> = *__begin; > <statement> > } > } > > If you have a temporary in your right hand of the ':' it will get lifetime- > extended by that __range reference. Any other temporaries will get destroyed. > > That means a cast works and a copy works. Returning a reference doesn't. > _______________________________________________ Development mailing list Development@qt-project.org http://lists.qt-project.org/mailman/listinfo/development