From f9ee637a1eef8ddc9b0992878903f1949adcfb00 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Tue, 2 Feb 2016 18:01:36 +0000 Subject: [PATCH] refs #583 as_const --- src/blackmisc/range.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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.