문제

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 ?

도움이 되었습니까?

해결책

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.

다른 팁

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top