From 1b671e9cb0bf805a9e669d923afdaf3009b56b18 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Sat, 6 Dec 2014 15:57:23 +0000 Subject: [PATCH] refs #247 CVariant::valueOrDefault convenience function. --- src/blackmisc/variant.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/blackmisc/variant.h b/src/blackmisc/variant.h index 8e6828c13..9e124db4f 100644 --- a/src/blackmisc/variant.h +++ b/src/blackmisc/variant.h @@ -120,6 +120,11 @@ namespace BlackMisc //! Synonym for value(). template T to() const { return m_v.value(); } + //! Returns the value converted to the type T, or a default if it can not be converted. + //! \details Parameter is passed by value to avoid odr-using the argument in case it is + //! an inline-initialized static const integral data member without a definition (§9.4.2/3). + template T valueOrDefault(T def) const { return canConvert() ? value() : def; } + //! Return the internal QVariant. const QVariant &getQVariant() const { return m_v; }