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

Access to a private reference? #12

Closed
blackbox-tech opened this issue Mar 28, 2023 · 2 comments
Closed

Access to a private reference? #12

blackbox-tech opened this issue Mar 28, 2023 · 2 comments

Comments

@blackbox-tech
Copy link

Awesome little header, is there a way to access a private member that is a reference?

It's not listed as a limitation, but I could not find a test for it.

I did some experiments in g but I got some compilation errors, for example:

struct A {
public:
    A() : foo(99), bar(foo) {}
private:
    int foo;
    int& bar;
};

ACCESS_PRIVATE_FIELD(A, int, bar)  // error: cannot create pointer to reference member
ACCESS_PRIVATE_FIELD(A, int&, bar) // error: cannot declare pointer to 'using Alias_PrivateAccessTag0 = int&' {aka 'int&'}

int& x = access_private::bar(a);
@blackbox-tech
Copy link
Author

I guess the answer is you can't as bar may not use any storage (like here is an alias for foo), so you can't uses its address to specialize the template?

@martong
Copy link
Owner

martong commented Mar 30, 2023

Hey, thank you for bringing this up. I believe that your conclusion is correct, a pointer to reference is illegal in C , thus we cannot instantiate the "getter" template. I'll update the limitations section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants