From e064bea8a9715a9cc36ff7971366350643f00fd0 Mon Sep 17 00:00:00 2001 From: John Maddock Date: Fri, 21 Jun 2013 15:58:03 +0000 Subject: [PATCH] Make unary + operator return by value - we can generate dangling references otherwise. [SVN r84863] --- include/boost/multiprecision/detail/et_ops.hpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/include/boost/multiprecision/detail/et_ops.hpp b/include/boost/multiprecision/detail/et_ops.hpp index a72dee90..3778e010 100644 --- a/include/boost/multiprecision/detail/et_ops.hpp +++ b/include/boost/multiprecision/detail/et_ops.hpp @@ -11,12 +11,19 @@ namespace boost{ namespace multiprecision{ // // Non-member operators for number: // -// Unary operators first: +// Unary operators first. +// Note that these *must* return by value, even though that's somewhat against +// existing practice. The issue is that in C++11 land one could easily and legitimately +// write: +// auto x = +1234_my_user_defined_suffix; +// which would result in a dangling-reference-to-temporary if unary + returned a reference +// to it's argument. While return-by-value is obviously inefficient in other situations +// the reality is that no one ever uses unary operator+ anyway...! // template -inline BOOST_CONSTEXPR const number& operator + (const number& v) { return v; } +inline BOOST_CONSTEXPR const number operator + (const number& v) { return v; } template -inline BOOST_CONSTEXPR const detail::expression& operator + (const detail::expression& v) { return v; } +inline BOOST_CONSTEXPR const detail::expression operator + (const detail::expression& v) { return v; } template inline detail::expression > operator - (const number& v) {