0

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?

9
  • Could std::ratio be of help for an implementation? Sep 25, 2020 at 22:29
  • @πάνταῥεῖ: I don't understand your comment — maybe it was meant for a different question?
    – jwd
    Sep 25, 2020 at 22:30
  • "I'm trying to implement a custom lua_Number type in Lua 5.3" That's not going to work. lua_Number must be a built-in type. The Lua codebase can't handle anything user-defined, since that's not how C works. Sep 25, 2020 at 22:30
  • @jwd No, it was for this one. You said you want to implement exact fractions instead of FP intrinsics, right? Sep 25, 2020 at 22:32
  • @NicolBolas why must it? For instance, PICO-8 uses a fixed-point implementation of Number in its Lua interpreter. Here's an open-source clone luaconf.h z8::fix32 impl. That's Lua 5.2; I'm trying to do similar in 5.3.
    – jwd
    Sep 25, 2020 at 22:33

1 Answer 1

0

Maybe the easiest way is to define that union which is needed for interoperability as:

union SomeUnion {
    uint32_t num;
    void * pFoo;

    void set(const MyNum& myNum) {
        num = *reinterpret_cast<const uint32_t*>(&myNum);
    }

    MyNum get() {
        return *reinterpret_cast<MyNum*>(&this->num);
    }
};

MyNum copies should be simply default:

MyNum(const MyNum& other) = default;
MyNum& operator=(const MyNum& other) = default;

This union is perfect for any low-level operations like volatile since it involves only primitive types; on the other hand it is easy to use the num field as a storage for a MyNum object using the getter and setter methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.