cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[SUGGESTION] Add the equivalent of C++23 "deducing this"

Open bluetarpmedia opened this issue 6 months ago • 15 comments

For the same motivations as in P0847, I'd like to be able to write the equivalent of the "deducing this" feature in Cpp2.

Here's one example where the feature would prevent duplicating code:

my_optional: <T> type = {
    has_value: bool = false;
    value:     T;

    get: (in this)    -> forward T = { assert(has_value); return value; }  // return const-ref
    get: (inout this) -> forward T = { assert(has_value); return value; }  // return mutable-ref
    get: (move this)  -> move    T = { assert(has_value); return value; }
}

Instead I'd like to write the get function once, with some kind of equivalent "deducing this" syntax that takes care of the this parameter and also the return type, e.g. along the lines of:

get: <Self>(Self _ this) -> _ = { assert(has_value); return value; }

The lowered C++ could duplicate the functions if it's targeting C++20, since the C++23 "deducing this" syntax won't be available.

If, in the future, cppfront supports some kind of cpp_standard flag (like proposed in #942) then the lowered code for C++23 could use the real "deducing this" feature.

Will your feature suggestion eliminate X% of security vulnerabilities of a given kind in current C++ code? No

Will your feature suggestion automate or eliminate X% of current C++ guidance literature? Yes, in the same way that "deducing this" does for C++.

bluetarpmedia avatar Aug 01 '24 06:08 bluetarpmedia