C++11 classes

Hans Aberg haberg-1 at telia.com
Thu Nov 17 00:45:43 CET 2011

On 16 Nov 2011, at 23:58, 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);
> x=42;

The move operator is only used for temporaries, though you can call it explicitly. See for example

>>>> 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?

It is the free store allocators that are time consuming. One allocation typically takes several tens of cycles, may even be hundreds, I am told.


More information about the gmp-discuss mailing list