C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Allow conversion of memfunc pointers to func pointers

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sat, 6 Jan 2024 12:05:04 +0000
On Fri, Jan 5, 2024 at 8:50 PM Jens Maurer wrote:
>
> This paper is talking about return values, not about (this) parameters.
>
> Why is it relevant to the discussion?


If there were any implementations of C++ in existence today that
passed the 'this' pointer in a separate register, I figured it would
be one of the four implementations that stored the address of the
return value in a separate register. I haven't looked into it. Also
things get a little more complicated when a function takes both a
'this' pointer and the address of the return value (because then you
have to make the decision of which one goes in 1st place and which one
goes in 2nd place).

Anyway. The C++ implementations that use thunks instead of 'an offset
for this' are much easier to code for than the implementations that
have an 'offset for this' embedded in the member function pointer.

GNU g++ and LLVM clang++ both implement member function pointers with
an embedded "offset for this". I have implemented
"std::memfunc_to_func" and "std::invoke_func_as_memfunc" for x86_64
GNU g++ and LLVM clang++ as follows:

   https://godbolt.org/z/aWo6s8175

My code works but it will break if you start using multiple
inheritance, because when "std::memfunc_to_func" is used, it discards
the 'offset for this'. And therefore when
"std::invoke_func_as_memfunc" is used, the 'this' pointer will be
wrong.

There is a possible workaround to this though. Even though the "offset
for this" is lost when "std::memfunc_to_func" is invoked, it could be
regained when "std::invoked_func_as_memfunc" is invoked (as it can be
determined from the type of the 2nd parameter -- i.e. the pointer to
the object). This might even be an enhancement as it means the 'this'
pointer will be correctly adjusted no matter what object you give it
(i.e. you're not limited to the type that was passed to
'std::memfunc_to_func').

Received on 2024-01-06 12:05:13