Problem with __gmp_expr
Marc Glisse
marc.glisse at inria.fr
Fri Jan 17 12:43:02 UTC 2014
(Torbjorn, you need -std=c++11)
On Fri, 17 Jan 2014, Ulrich Drepper wrote:
> In C++11 there are several situations where the result of a
> computation using mp?_class objects is not immediately casted to a
> mp?_class type.
It is well known that expression templates misbehave in some cases and
that C++11 increased those significantly. There are proposals in
discussion (often called "operator auto", although the syntax will be
different) to let class implementers change the resolution of auto, but
that won't even be in the next version of the standard, so not soon.
> I haven't investigated further why this causes problems but there are
> some.
Generally, I am in favor of expanding the "don't do that" section in:
https://gmplib.org/manual/C_002b_002b-Interface-Limitations.html
In this particular case (I haven't checked closely yet), it may be that
things "work" with other expression template libraries whose expressions
use copies instead of references. In gmp, -m*a is a neg expr that stores a
ref to a mult expr that stores references to m and a. Since you managed to
postpone the evaluation of the expression, the mult expr went out of scope
(older versions of gcc don't crash because they are not good at reusing
space from dead objects). Other libraries would have a big neg expr that
embeds a copy of the mult expr, and only mp?_class is spared the copying.
It has advantages. It also seems (didn't try it myself) to make it harder
for the compiler to eliminate all this meta-stuff. It may be worth
re-evaluating (I don't have time to do it myself soon).
--
Marc Glisse
More information about the gmp-bugs
mailing list