literals for an int128 class

Discussion in 'C++' started by cps, May 3, 2011.

  1. cps

    cps Guest

    I am considering writing an int128 class as a programming exercise.
    Before I start (and I have a fair idea of how to do it) I was
    wondering what I can do about literals and the assignment operator. I
    would like to be able to use integer literals with the assignment and
    other operators with literals that are sized at 128 bits.

    E.g. I'd like to be able to do:

    uint128 myInt128 = 184467440737095516160;


    Note that the foregoing literal would produce an error (correctly
    so) if I attempted to assign it to an uint64 because it is (2^64) *
    10.

    Is there any way I can set it up to deal with large literals?

    Or am I going to have to use strings and parse them?
     
    cps, May 3, 2011
    #1
    1. Advertising

  2. On 5/3/2011 4:39 PM, cps wrote:
    > I am considering writing an int128 class as a programming exercise.
    > Before I start (and I have a fair idea of how to do it) I was
    > wondering what I can do about literals and the assignment operator.


    Judging from the excerpt below, you're talking about the symbol '=' used
    for initialization of objects at the time of their definition, and *NOT*
    about the assignment operator.

    > I
    > would like to be able to use integer literals with the assignment and
    > other operators with literals that are sized at 128 bits.
    >
    > E.g. I'd like to be able to do:
    >
    > uint128 myInt128 = 184467440737095516160;


    That is called initialization.

    > Note that the foregoing literal would produce an error (correctly
    > so) if I attempted to assign it to an uint64 because it is (2^64) *
    > 10.
    >
    > Is there any way I can set it up to deal with large literals?


    Not portably. Literals are defined by the standard/compiler and unless
    your compiler already provides a 128 bit signed or unsigned integer
    literal, you can't introduce it. If 'uint128' is totally your own
    custom class, then your best bet is to introduce a constructor from a
    character string (uint128::uint128(const char*)) and surround those
    digits with double quotes:

    uint128 myInt128("184467440737095516160");

    *Some* compilers can offer built-in integral types of that size and with
    those they usually offer a way to specify a literal by means of some
    suffix. For instance, MS used to have i64 suffix before 64 bit targets
    became common:

    int64_t ms64bitInteger = 1234567890i64; // if memory serves

    > Or am I going to have to use strings and parse them?


    Most likely.

    T
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 3, 2011
    #2
    1. Advertising

  3. cps

    Ian Collins Guest

    On 05/ 4/11 08:39 AM, cps wrote:
    > I am considering writing an int128 class as a programming exercise.
    > Before I start (and I have a fair idea of how to do it) I was
    > wondering what I can do about literals and the assignment operator. I
    > would like to be able to use integer literals with the assignment and
    > other operators with literals that are sized at 128 bits.
    >
    > E.g. I'd like to be able to do:
    >
    > uint128 myInt128 = 184467440737095516160;
    >
    >
    > Note that the foregoing literal would produce an error (correctly
    > so) if I attempted to assign it to an uint64 because it is (2^64) *
    > 10.
    >
    > Is there any way I can set it up to deal with large literals?
    >
    > Or am I going to have to use strings and parse them?


    The only way a system could have 128 bit literals would be if it had a
    128 bit type!

    You'll have to settle for strings.

    --
    Ian Collins
     
    Ian Collins, May 3, 2011
    #3
  4. On 3 mai, 22:39, cps <> wrote:
    > I am considering writing an int128 class as a programming exercise.
    > Before I start (and I have a fair idea of how to do it) I was
    > wondering what I can do about literals and the assignment operator.  I
    > would like to be able to use integer literals with the assignment and
    > other operators with literals that are sized at 128 bits.
    >
    > E.g. I'd like to be able to do:
    >
    >      uint128 myInt128 = 184467440737095516160;
    >
    >      Note that the foregoing literal would produce an error (correctly
    > so) if I attempted to assign it to an uint64 because it is (2^64) *
    > 10.
    >
    >      Is there any way I can set it up to deal with large literals?
    >
    >     Or am I going to have to use strings and parse them?


    Parsing it from string is however a good idea.

    With C++0x, you will be able to write:

    uint128 operator "" ulll(const char * string_values, size_t
    num_chars)
    {
    return uint128::parse(string_values,num_chars);
    }

    uint128 myInt128 = 184467440737095516160ulll;

    --
    Michael
     
    Michael Doubez, May 4, 2011
    #4
  5. cps

    Ian Collins Guest

    On 05/ 4/11 08:38 PM, Michael Doubez wrote:
    > On 3 mai, 22:39, cps<> wrote:
    >> I am considering writing an int128 class as a programming exercise.
    >> Before I start (and I have a fair idea of how to do it) I was
    >> wondering what I can do about literals and the assignment operator. I
    >> would like to be able to use integer literals with the assignment and
    >> other operators with literals that are sized at 128 bits.
    >>
    >> E.g. I'd like to be able to do:
    >>
    >> uint128 myInt128 = 184467440737095516160;
    >>
    >> Note that the foregoing literal would produce an error (correctly
    >> so) if I attempted to assign it to an uint64 because it is (2^64) *
    >> 10.
    >>
    >> Is there any way I can set it up to deal with large literals?
    >>
    >> Or am I going to have to use strings and parse them?

    >
    > Parsing it from string is however a good idea.
    >
    > With C++0x, you will be able to write:
    >
    > uint128 operator "" ulll(const char * string_values, size_t
    > num_chars)
    > {
    > return uint128::parse(string_values,num_chars);
    > }
    >
    > uint128 myInt128 = 184467440737095516160ulll;


    Neat. In which section of the standard is this described?

    --
    Ian Collins
     
    Ian Collins, May 4, 2011
    #5
  6. On 4 mai, 11:15, Ian Collins <> wrote:
    > On 05/ 4/11 08:38 PM, Michael Doubez wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > On 3 mai, 22:39, cps<>  wrote:
    > >> I am considering writing an int128 class as a programming exercise.
    > >> Before I start (and I have a fair idea of how to do it) I was
    > >> wondering what I can do about literals and the assignment operator.  I
    > >> would like to be able to use integer literals with the assignment and
    > >> other operators with literals that are sized at 128 bits.

    >
    > >> E.g. I'd like to be able to do:

    >
    > >>       uint128 myInt128 = 184467440737095516160;

    >
    > >>       Note that the foregoing literal would produce an error (correctly
    > >> so) if I attempted to assign it to an uint64 because it is (2^64) *
    > >> 10.

    >
    > >>       Is there any way I can set it up to deal with large literals?

    >
    > >>      Or am I going to have to use strings and parse them?

    >
    > > Parsing it from string is however a good idea.

    >
    > > With C++0x, you will be able to write:

    >
    > > uint128  operator "" ulll(const char * string_values, size_t
    > > num_chars)
    > > {
    > >    return uint128::parse(string_values,num_chars);
    > > }

    >
    > > uint128 myInt128 = 184467440737095516160ulll;

    >
    > Neat.  In which section of the standard is this described?


    In the current draft (n3242)
    2.14.8 User-defined literals [lex.ext]

    --
    Michael
     
    Michael Doubez, May 6, 2011
    #6
  7. On 5/6/2011 8:00 AM, Pete Becker wrote:
    > On 2011-05-06 04:03:13 -0400, Michael Doubez said:
    >
    >>
    >> In the current draft (n3242)
    >>

    >
    > The current draft is N3291.
    >


    Huh...

    One would expect it be linked to on the Committee's page

    http://www.open-std.org/jtc1/sc22/wg21/

    , yet the link still points to n3242... Just to let you know... In
    case you somehow can influence what's put on that web page...

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 6, 2011
    #7
  8. On 6 mai, 14:00, Pete Becker <> wrote:
    > On 2011-05-06 04:03:13 -0400, Michael Doubez said:
    >
    > > In the current draft (n3242)

    >
    > The current draft is N3291.


    Thanks. I just grabbed it from WG21 page.

    Is there any hope that c++1x or C++2x will make it to N4242 ?

    --
    Michael
     
    Michael Doubez, May 6, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Al Wilkerson

    literals

    Al Wilkerson, Sep 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,606
    Jonathan Allen
    Sep 25, 2004
  2. Purush
    Replies:
    4
    Views:
    1,665
    Purush Rudrakshala
    Apr 13, 2005
  3. E11
    Replies:
    1
    Views:
    4,788
    Thomas Weidenfeller
    Oct 12, 2005
  4. John Goche
    Replies:
    8
    Views:
    16,466
  5. KM
    Replies:
    0
    Views:
    88
Loading...

Share This Page