-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Embind and references #3480
Comments
I also realized that returning a pointer is different from returning a reference. In my eyes this is a bit an unexpected behavior. |
Will be nice to implement this feature! |
I have been making tests to implement this feature in the last few days. The simplest way I have to do this client-side is : template <typename T, T> struct proxy;
template <typename T, typename R, typename ...Args, R (T::*mf)(Args...)>
struct proxy<R (T::*)(Args...), mf>
{
using ptr = typename std::add_pointer<typename std::remove_reference<R>::type>::type;
static ptr call(T & obj, Args &&... args)
{
return &((obj.*mf)(std::forward<Args>(args)...));
}
};
#define RETURN_POINTER(value) &proxy<decltype(value), (value)>::call This way, you can do I have tried several ways of adding support for reference in embind, but every time, it breaks something existing. For now, references are treated as regular objects, so they are sometimes copied and sometimes not. There also seem to be tests in If it is not possible to transparent support for references, at least having a policy similar to Edit : I opened PR #5037 to show my progress. |
what if the implementation is specialized for std::reference_wrapper and gets unwrapped when creating the JS object? I don't see the issue of forcing people to return std::reference_wrapper when they want the actual reference. If restricting it to non-trivial types is needed, it's as simple as adding a trait that prevents those to be returned as references. The internal implementation can unwrap those to pointers, that get translated to js objects in the end (exactly the same thing as when you return a pointer from embind functions) |
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant. |
For those suffering template <typename T, T> struct proxy;
template <typename T, typename R, typename ...Args, R (T::*mf)(Args...) const>
struct proxy<R (T::*)(Args...) const, mf>
{
using ptr = typename std::add_pointer<typename std::remove_reference<R>::type>::type;
static ptr call(T & obj, Args &&... args)
{
return &((obj.*mf)(std::forward<Args>(args)...));
}
};
#define RETURN_POINTER(value) &proxy<decltype(value), (value)>::call |
FWIW, I did some work on references in #17602. I ultimately closed that issue in favor of trying to add a more explicit ownership flags. |
Hello,
I made a simple API that uses method chaining in C++. This is achieved by returning a reference from member function, like this :
So, I can write
Chain().foo().bar()
.Now, when binding this with embind, references are copies of the original object, so memory leaks since I should call
delete
after each function call, preventing chaining.I know I have several solutions to deal with this, namely using pointers, either directly in my class or in the embind bindings, but those are not very elegant solutions.
Is there any reason for which embind makes copies when using reference instead of using the same pointer (at least for objects) ?
If it is possible to implement reference support, I am willing to participate. Where should I start ?
The text was updated successfully, but these errors were encountered: