I am writing a class with various operator overloading which should be a drop-in replacement for float (or similar) in some "external" code.
In particular, there are 3 example usages which I'd like to support (see below), and I can't find a way to define my class in a way that satisfies all 3.
Is it possible?
Aside: In truth, I'm trying to implement a custom lua_Number type in Lua 5.3, which is typically either float or double. I am compiling Lua as C++, and trying to use a fixed-point representation instead. While it is true that I can alter the "external" code (since it's just embedded Lua), I would very much rather not (:
Live code link, if you want to play around: https://godbolt.org/z/37c6nj
Here are the 3 use cases I need to support:
Example 1: simple usage where MyNum is a union member. This means I cannot have exotic copy constructors and such, since "non-trivial" versions of those would cause the union to be ill-formed (without modification).
SomeUnion x;
x.num = 123.0f;
SomeUnion y = x;
Example 2: MyNum is a member of a union (as above), and that union is the member of a struct. I'm not sure exactly how this is different from the first example, b ut it is the final sticking point in my current version of the code. This is the bit that doesn't currently compile in the linked example.
SomeStruct s1;
s1.u.num = x.num;
SomeStruct s2;
// With all the code as-is, this is the line that fails:
// error: use of deleted function 'SomeStruct& SomeStruct::operator=(const SomeStruct&)'
// note: 'SomeStruct& SomeStruct::operator=(const SomeStruct&)' is implicitly deleted because the default definition would be ill-formed
s2 = s1;
Example 3: Usage concerning volatile. This requires a custom operator=.
volatile SomeUnion v;
// If we don't have a custom operator= with 'volatile' qualifier, we get:
// ERROR: passing 'volatile MyNum' as 'this' argument discards qualifiers [-fpermissive]
v.num = x.num;
If I remove the volatile implementation of operator=, then Example1 and Example2 compile, but Example3 does not.
If I include the volatile version of operator=, then Example1 and Example3 compile, but Example2 does not.
Is there some way to make them all work?
Or, failing that, any ideas for a "minimally invasive" set of changes to the external code to allow it to work?
std::ratiobe of help for an implementation?lua_Numbertype in Lua 5.3" That's not going to work.lua_Numbermust be a built-in type. The Lua codebase can't handle anything user-defined, since that's not how C works.