Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r84863 - trunk/boost/multiprecision/detail
From: john_at_[hidden]
Date: 2013-06-21 11:58:04


Author: johnmaddock
Date: 2013-06-21 11:58:03 EDT (Fri, 21 Jun 2013)
New Revision: 84863
URL: http://svn.boost.org/trac/boost/changeset/84863

Log:
Make unary + operator return by value - we can generate dangling references otherwise.

Text files modified:
   trunk/boost/multiprecision/detail/et_ops.hpp | 13 ++++++++++---
   1 files changed, 10 insertions(+), 3 deletions(-)

Modified: trunk/boost/multiprecision/detail/et_ops.hpp
==============================================================================
--- trunk/boost/multiprecision/detail/et_ops.hpp Fri Jun 21 11:53:09 2013 (r84862)
+++ trunk/boost/multiprecision/detail/et_ops.hpp 2013-06-21 11:58:03 EDT (Fri, 21 Jun 2013) (r84863)
@@ -11,12 +11,19 @@
 //
 // 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 <class B, expression_template_option ExpressionTemplates>
-inline BOOST_CONSTEXPR const number<B, ExpressionTemplates>& operator + (const number<B, ExpressionTemplates>& v) { return v; }
+inline BOOST_CONSTEXPR const number<B, ExpressionTemplates> operator + (const number<B, ExpressionTemplates>& v) { return v; }
 template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
-inline BOOST_CONSTEXPR const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& operator + (const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& v) { return v; }
+inline BOOST_CONSTEXPR const detail::expression<tag, Arg1, Arg2, Arg3, Arg4> operator + (const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& v) { return v; }
 template <class B>
 inline detail::expression<detail::negate, number<B, et_on> > operator - (const number<B, et_on>& v)
 {


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk