Problem with __gmp_expr

Marc Glisse marc.glisse at
Fri Jan 17 13:18:01 UTC 2014

On Fri, 17 Jan 2014, Marc Glisse wrote:

> 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).

The following patch (only tested on this example) handles your example 
with and without LAMBDA.

@@ -1280,10 +1280,10 @@
    typedef T ref_type;

-template <class T, class U>
-struct __gmp_resolve_ref<__gmp_expr<T, U> >
+template <class T>
+struct __gmp_resolve_ref<__gmp_expr<T, T> >
-  typedef const __gmp_expr<T, U> & ref_type;
+  typedef const __gmp_expr<T, T> & ref_type;

@@ -1398,7 +1398,7 @@
  template <class T, class Op>
  struct __gmp_unary_expr
-  const T &val;
+  typename __gmp_resolve_ref<T>::ref_type val;

    __gmp_unary_expr(const T &v) : val(v) { }

Marc Glisse

More information about the gmp-bugs mailing list