1

I've got Decimal class which implements decimal floating point arithmetic. It can be initialized with an integral arithmetic type or a fractional digit stored as a string. Build-in floating point types are deliberately avoided because of their approximative nature. Such approach works well but fractional digits coded as strings look awkward. Is it possible to use somehow C++ literals to build strings (or binary representation) behind the scene?

Decimal a{"1.254684987"}; // current initialization
Decimal b{1.254684987_dec}; // desired way
sliser
  • 1,616
  • 11
  • 14

1 Answers1

5

Given that you already have a c'tor taking a C-string, simply writr something like

Decimal operator "" _dec(const char* c){
    return Decimal{c}; // Assuming an explicit c'tor
}

auto whatever = 12.3_dec;

Of course, you need a C++11 compiler for this to work.

iBug
  • 32,728
  • 7
  • 79
  • 117
Massimiliano Janes
  • 5,477
  • 1
  • 9
  • 22