C++11 user-defined literals

Gabriel Dos Reis gdr at integrable-solutions.net
Wed Mar 7 15:53:15 CET 2012

On Wed, Mar 7, 2012 at 5:59 AM, Marc Glisse <marc.glisse at inria.fr> wrote:
> On Wed, 7 Mar 2012, Gabriel Dos Reis wrote:
>> On Wed, Mar 7, 2012 at 2:27 AM, Marc Glisse <marc.glisse at inria.fr> wrote:
>>> On Tue, 6 Mar 2012, Gabriel Dos Reis wrote:
>>>> I believe it is actually very useful (assuming you have a C++11 compiler
>>>> such as GCC-4.7.x) for reason you stated: is your integer literal does not
>>>> fit 'unsigned long' (and you would actually like to use 'long long' or
>>>> 'unsigned long long') you are kind of stuck with the current (limited)
>>>> interface.
>>> Do you mean that gmp should get 'long long' constructors?
>> That will probably satisfy the C API, but my suggestion is that the
>> C++ API should get the user-defined literal constructor -- you would
>> not have to worry whether you need to add the 'long long long'
>> constructor :-)
> Well, the user-defined literal will internally use a string and thus be
> slower (that kind of construction should rarely matter for the performance,
> but who knows... and you can't even overload it so it calls a long long
> version when it fits and a string version when it doesn't). And people will
> still want interoperability with long long.

Yes, those are valid observations.  The way I see it is this: the
user-defined literal
version always works.  If a user wants to be specific, it would user
the unsigned int
constructor or the long long constructor (when it is added.)

>>> Unless I am missing something, whenever people write something_mpz, it
>>> will
>>> be equivalent to mpz_class(something) or mpz_class("something"), which is
>>> quite readable and not much longer.
>> Yes, I find the suffix notation more readable than the string notation.
> Ok, so the only benefit is readability, that was my main question, thanks.
> By the way, any opinion on the operator name? (I am not sure about _mpz)

I am fine with it, if it sounds or looks good to you.  When using GMP I believe
mpz and mpq are part of the standard vocabulary.  You cloud claim
_integer or (or _bigint)
but that sounds mouthful.

> Also, for mpq, the 2 things that seem to make sense are -123_mpq/42 and
> "-123/42"_mpq. Would you want both?

I would just take one (the non-string version.)  The idea of
user-defined literals
is to give a natural notation...

> If I provide a (const char*, length)
> overload, it seems to hide the (const char*) numeric overload from g++, and
> in the reverse direction, the "-123/42"_mpq version won't use the (const
> char*) overload :-(

More information about the gmp-discuss mailing list