Question

I am working on a class that deals with big numbers in C++.

Thing is I want it to be able to do a normal initialisation like:

Largeint A = 1934804692305674830675460730458673084576;

Instead of having to put the number between " ".

How should I go about achieving that?

Edit due to comments:

I know how to work with big numbers and do operations with them. The thing i was asking for is that i just don't want it too look like a string when giving it a value. Why? Just because.

And if integer literals are bound to the compiler settings, is there anyway i can go around this?

Both answeres are interesting and UDL are cool :D But, is there a way to use UDL without having to put a suffix at the end ?

Was it helpful?

Solution

With C++11, we can make User-defined literals

Largeint operator "" _largeint(const char* literal_string)
{
    Largeint largeint;

    // initialize largeint with literal string content;
    return largeint;
}

or, if you prefer the variadic template

template<char... Cs> Largeint operator "" _largeint();

And then use it:

Largeint largeint = 123456789012345678901234567890_largeint;

You may use a more appropriate suffix name.

OTHER TIPS

You could use a macro like this:

#define MakeLargeint(VAR, N) Largeint VAR = #N;

and define Largeints constructor to take a string.

So your line becomes:

MakeLargeint(A ,1934804692305674830675460730458673084576)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top