Re: [boost] [Multiprecision] Does MP use references internally?
Nearly all operators, besides assigning and dereferencing ones, are supposed to return by value. Changing that, even with the “innocuous” (unary)+, is surprising. Daryle W. Sent from Windows Mail From: John Maddock Sent: Thursday, June 20, 2013 4:13 AM To: boost@lists.boost.org
Is Boost.MP giving users references-to-types instead of class types when requesting a numeric type? Pointers may be objects in and of themselves, but references aren't; they're just aliases; trying to intentionally use them as first-class objects is arguably broken.
I don't see why this is a problem, but the first error is coming from: auto const aa = +a, bb = +b; Now, in Boost.Multiprecision the unary operator+ is a noop that returns a reference to self, could that be a cause of the issue?
Nearly all operators, besides assigning and dereferencing ones, are supposed to return by value. Changing that, even with the “innocuous” (unary)+, is surprising.
This came up in review actually with no good solution - the main issue (apart from no one ever using unary + anyway) is that pretty much every numeric-class type ever written has done it that way. However... in the C++11 world I can think of one legitimate use case that's broken by this - and that's when you have user-defined literals: auto x = +1234_my_suffix; which results in a dangling-reference-to-temporary. So I'll change this shortly, Regards, John.
John Maddock wrote:
However... in the C++11 world I can think of one legitimate use case that's broken by this - and that's when you have user-defined literals:
auto x = +1234_my_suffix;
which results in a dangling-reference-to-temporary.
I think we can avoid dangling references by adding a unary plus operator overload (that returns a value) for rvalues. Regards, Michel
On 21 June 2013 06:56, John Maddock
Nearly all operators, besides assigning and dereferencing ones, are
supposed to return by value. Changing that, even with the “innocuous” (unary)+, is surprising.
This came up in review actually with no good solution - the main issue (apart from no one ever using unary + anyway)
I wouldn't quite say that... My use case is: unsigned char u = 100; std::cout << +u << std::endl; (since it is far shorter than all the alternatives). -- Nevin ":-)" Liber mailto:nevin@eviloverlord.com (847) 691-1404
participants (4)
-
Daryle Walker
-
John Maddock
-
Michel Morin
-
Nevin Liber