cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[SUGGESTION] Allow `swap` to be a hidden friend

Open bluetarpmedia opened this issue 6 months ago • 1 comments

As I understand it, there's no friend keyword in Cpp2, but some operators are emitted as hidden friends in a type:

  • streaming operators >> and <<
  • binary arithmetic operators +, -, *, /, %

I think it'd be useful to add swap to this list if the type contains a non-member binary function with inout params of that type:

MyObject: type = {
    value: std::string = ();
    swap: (inout a: MyObject, inout b: MyObject) = {
        std::swap(a.value, b.value);
    }
}

Currently cppfront lowers that swap to:

public: static auto swap(MyObject& a, MyObject& b) -> void;

But I'd like:

friend auto swap(MyObject& a, MyObject& b) -> void;

(Maybe add noexcept too?)

That way clients can call the hidden friend via ADL:

o1: MyObject;
o2: MyObject;
using std::swap;
swap(o1, o2);

bluetarpmedia avatar Aug 05 '24 04:08 bluetarpmedia