vpiotr / decimal_for_cpp

Decimal data type for C++
273 stars 68 forks source link

how to initialize in struct declaration #58

Closed omerbrandis closed 1 year ago

omerbrandis commented 1 year ago

Hello Piotr

First I must thank you for creation, its really a must in the cpp space.

I'm using 1.18, and am unable to compile the following struct MyStruct { decimal<2> myVariable(0); }

also can't struct MyStruct { decimal<2> myVariable = 0; } error : could not convert ‘0’ from ‘int’ to ‘dec::decimal<2>’ ^

I can set a value in the constructor mil. this is a decent workaround , but less convenient for me at this time.

please advise, Omer.

Kvaz1r commented 1 year ago

Try this:

struct MyStruct
{
    decimal<2> myVariable{ 0 };
};
omerbrandis commented 1 year ago

it works , :-)

thank you Kvaz1r,

can you please tell me a bit more about what/why this is ?

thanks, Omer.

vpiotr commented 1 year ago

@omerbrandis The "0" is just an example or a real value? It looks like you are trying to investigate rules of initialization which can be very complicated - if you do not provide your own constructor. Solution from @Kvaz1r probably works but I would suggest to add a default constructor with initialization of myVariable.

// untested
struct MyStruct
{
  decimal<2> myVariable;
  MyStruct(): myVariable(0) { }
}

See rules (I have already a headache after reading it):

omerbrandis commented 1 year ago

thanks !