diff --git a/src/blackmisc/range.h b/src/blackmisc/range.h index a17026a52..2e754d5f9 100644 --- a/src/blackmisc/range.h +++ b/src/blackmisc/range.h @@ -38,6 +38,18 @@ namespace BlackMisc template class CRange; + /*! + * Own implementation of C++17 std::as_const. Adds const to any lvalue. + * Useful with non-mutating range-for loops to avoid unnecessary detachment of Qt implicitly shared containers. + * Does not allow rvalues, as that could easily lead to undefined behaviour. + */ + //! @{ + template + Q_CONSTEXPR typename std::add_const::type &as_const(T &v) Q_DECL_NOEXCEPT { return v; } + template + void as_const(const T &&) = delete; + //! @} + /*! * Any container class with begin and end iterators can inherit from this CRTP class * to gain some useful algorithms as member functions.