Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > literals for an int128 class

Reply
Thread Tools

literals for an int128 class

 
 
cps
Guest
Posts: n/a
 
      05-03-2011
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?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-03-2011
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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-03-2011
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
 
Reply With Quote
 
Michael Doubez
Guest
Posts: n/a
 
      05-04-2011
On 3 mai, 22:39, cps <chrissus...@gmail.com> 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:arse(string_values,num_chars);
}

uint128 myInt128 = 184467440737095516160ulll;

--
Michael
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-04-2011
On 05/ 4/11 08:38 PM, Michael Doubez wrote:
> On 3 mai, 22:39, cps<chrissus...@gmail.com> 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:arse(string_values,num_chars);
> }
>
> uint128 myInt128 = 184467440737095516160ulll;


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

--
Ian Collins
 
Reply With Quote
 
Michael Doubez
Guest
Posts: n/a
 
      05-06-2011
On 4 mai, 11:15, Ian Collins <ian-n...@hotmail.com> wrote:
> On 05/ 4/11 08:38 PM, Michael Doubez wrote:
>
>
>
>
>
>
>
>
>
> > On 3 mai, 22:39, cps<chrissus...@gmail.com> *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:arse(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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      05-06-2011
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
 
Reply With Quote
 
Michael Doubez
Guest
Posts: n/a
 
      05-06-2011
On 6 mai, 14:00, Pete Becker <p...@versatilecoding.com> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class of numeric literals KM Ruby 0 04-11-2007 03:45 PM
Java: byte literals and short literals John Goche Java 8 01-17-2006 11:12 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Generic class literals - e.g,, Class<Map<String, Integer>>.class Purush Java 4 04-13-2005 08:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57