Skip to content
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

Specify name of class that static method is defined on #464

Closed
dtolnay opened this issue Nov 12, 2020 · 1 comment
Closed

Specify name of class that static method is defined on #464

dtolnay opened this issue Nov 12, 2020 · 1 comment
Labels

Comments

@dtolnay
Copy link
Owner

dtolnay commented Nov 12, 2020

Question from @nixbox:

I am trying to use CXX. How do I declare a C++ static method under the cxxbridge extern "C"? More specifically, how can I specify the name of the class that static method is defined on? Would cxx_name take "Class::StaticMethod"?

@dtolnay
Copy link
Owner Author

dtolnay commented Nov 12, 2020

This is not implemented yet but it just came up recently in #447 too. I do not like #[cxx_name = "Class::StaticMethod"] because the common case would be that you want either both sides to be methods or neither side to be a method; cxx_name makes it seem like you're dealing with a method on the C++ side and a free function on the Rust side.

In #447 I proposed:

extern "C++" {
    type Class;

    #[Self = "Class"]
    fn StaticMethod();
}

But for now the workaround is to add a header that contains a shim for the static member function you want to call.

extern "C++" {
    include!("my/shims.h");

    type Class;

    fn Class_StaticMethod();
}
// shims.h
#pragma once
#include "Class.h"

inline void Class_StaticMethod() {
  Class::StaticMethod();
}

@dtolnay dtolnay closed this as completed Nov 15, 2020
davvid added a commit to davvid/ptex-bind that referenced this issue Jul 26, 2023
cxx does not currently support static class methods:
dtolnay/cxx#464 (comment)

Add a free-standing function for use from the sys bindings.
davvid added a commit to davvid/ptex-bind that referenced this issue Jul 26, 2023
cxx does not currently support static class methods:
dtolnay/cxx#464 (comment)

Add a free-standing function for use from the sys bindings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant