[Development] Proposal: Deprecating platforms in Qt 5.6 that don't support lambda

Matthew Woehlke mw_triad at users.sourceforge.net
Thu Apr 30 22:04:18 CEST 2015


On 2015-02-20 14:42, Thiago Macieira wrote:
> On Friday 20 February 2015 12:53:24 Matthew Woehlke wrote:
>>   for (auto const i : qtEnumerate(map))
>>
>> Maybe it would be nice for Qt to provide one or both of these?
> 
> Sounds easy enough. Want to give it a try?

I *finally* got permission to share this. Sorry it took so long.

The attached code, excluding the copyright notices, is hereby placed
into the Public Domain. Permission is also granted to use the attached
code under either the BSD license, as stated in the files themselves, or
pursuant to Kitware's CLA with Qt.

It is my hope that this is useful to other people. (Also that the first
one finds its way into STL eventually :-), though that's a bit OT for here.)

These are both C++11 code. At minimum, they'll need brace-initialization
changed to parentheses-initialization in order to build in C++03 mode.
However, qtIndexRange also uses trailing return type specification, and
I'm not sure it's possible to avoid that without losing type deduction,
which sort-of defeats the purpose. There are also lots of elided type
specifiers, though those are easy enough to add.

Personally, I wouldn't consider it a terrible loss if these were only
available in C++11-or-later mode, since they're intended to be used with
range-based for.

> Note that this should also work for foreach:
> 
> 	foreach (const auto i, qtEnumerate(map))

I'm not sure if it will or not; it was designed to work in range-based
for, i.e. it supplies begin() and end(). I'm not sure if that's enough
to automagically work in Q_FOREACH.

Thiago, do you want me to take another look at integrating these into Qt
proper, or do you have it in hand?

-- 
Matthew
-------------- next part --------------
/*
Copyright 2015 Kitware, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

 * Neither name of Kitware, Inc. nor the names of any contributors may be used
   to endorse or promote products derived from this software without specific
   prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Based on code written by Matthew Woehlke and used by permission.
*/

#ifndef __qtEnumerate_h
#define __qtEnumerate_h

//-----------------------------------------------------------------------------
template <typename Container> class qtEnumerator
{
public:
    class iterator;

    qtEnumerator(Container const& container) : c(container) {}

    iterator begin() const { return {c.begin()}; }
    iterator end() const { return {c.end()}; }

protected:
    Container const& c;
};

//-----------------------------------------------------------------------------
template <typename Container> class qtEnumerator<Container>::iterator
{
public:
    using Iterator = typename Container::const_iterator;

    Iterator operator*() const { return i; }
    iterator& operator++() { ++i; return *this; }

    bool operator==(iterator const& other) const
    { return i == other.i; }

    bool operator!=(iterator const& other) const
    { return i != other.i; }

protected:
    friend class qtEnumerator<Container>;
    iterator(Iterator const& iter) : i{iter} {}

    Iterator i;
};

//-----------------------------------------------------------------------------
template <typename Container>
qtEnumerator<Container> qtEnumerate(Container const& container)
{
    return {container};
}

#endif
-------------- next part --------------
/*
Copyright 2015 Kitware, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

 * Neither name of Kitware, Inc. nor the names of any contributors may be used
   to endorse or promote products derived from this software without specific
   prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Based on code written by Matthew Woehlke and used by permission.
*/

#ifndef __qtIndexRange_h
#define __qtIndexRange_h

//-----------------------------------------------------------------------------
template <typename T> class qtIndexRangeType
{
public:
    class iterator;

    qtIndexRangeType(T count) : End{count} {}

    iterator begin() const { return {T{0}}; }
    iterator end() const { return {End}; }

protected:
    T End;
};

//-----------------------------------------------------------------------------
template <typename T> class qtIndexRangeType<T>::iterator
{
public:
    T operator*() const { return Value; }
    iterator& operator++() { ++Value; return *this; }

    bool operator==(iterator const& other) const
    { return Value == other.Value; }

    bool operator!=(iterator const& other) const
    { return Value != other.Value; }

protected:
    friend class qtIndexRangeType<T>;
    iterator(T value) : Value{value} {}

    T Value;
};

//-----------------------------------------------------------------------------
template <typename T> auto qtIndexRange(T count) -> qtIndexRangeType<T>
{
    return {count};
}

#endif


More information about the Development mailing list