C++11 classes

Marc Glisse marc.glisse at inria.fr
Wed Nov 16 23:58:35 CET 2011

On Wed, 16 Nov 2011, Hans Aberg wrote:

> On 16 Nov 2011, at 13:11, Marc Glisse wrote:
>>>> Note that without lazy initialization, gmp doesn't benefit from moves as much as it could.
>>> I'm not sure what you have in mind here.
>> The move constructor still needs to call mp*_init, because it has to leave the moved-from object in a usable state, so there is still an allocation.
> The move constructor just moves the data, and sets the original to a state which is not affected by delete. The other constructors do the allocation.

No. The following has to work if you want to be able to use your type in 
general contexts (maybe even just to put it in a std::vector):
mpz_class x;
mpz_class y=std::move(x);

>>> Further copying/moving can be done by using a reference count.
>> That's a different approach. If you use reference counting, move operations will bring you less.
> With a GC, like the Hans Boehm one, one might not need to have move constructors at all. A copy constructor may just copy over a pointer, keeping in mind that objects are references.

Yes, that's yet another possibility. There are also more intrusive 
optimizations you can consider with a gc, for instance for mpz_*div_2exp 
you could sometimes just move the pointer.

On Wed, 16 Nov 2011, Hans Aberg wrote:

> On 16 Nov 2011, at 12:23, Marc Glisse wrote:
>>> and then testing with a conversion of the pi program. In the most optimized version, the overhead is less than 10% relative C or C compile as C++,
>> I believe you can get 0 overhead with gmpxx.
> According to the GMP manual, sec. 12.1, "C++ Interface General", in the expression c = a+b, there no use of a temporary, but there is in a = b*c+d*e.
> So there is overhead there.
> In my code, they both call the move assignment operator. So there is a tradeoff: introduction of some move assignment operators, but replacing copy with move.

Wow. "Their code uses a temporary, but my code doesn't use copies". 
Comparing apples and oranges?

Marc Glisse

More information about the gmp-discuss mailing list