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

[variant.visit] C-style casting in visit member #7130

Open
hewillk opened this issue Jul 10, 2024 · 3 comments
Open

[variant.visit] C-style casting in visit member #7130

hewillk opened this issue Jul 10, 2024 · 3 comments

Comments

@hewillk
Copy link
Contributor

hewillk commented Jul 10, 2024

The two member visit have c-style casting (V)self in [variant.visit].
From my understanding, using C-style casting is not a best practice and should be avoided.
Would be better to use static_cast (not clear whether the two are completely equivalent in such cases)?

@CaseyCarter
Copy link
Contributor

CaseyCarter commented Jul 10, 2024

C-style casts are necessary in explicit object member functions to access members of a potentially inaccessible base class (https://godbolt.org/z/Gb6vzzY9r). This C-style cast is load-bearing.

@hewillk
Copy link
Contributor Author

hewillk commented Jul 10, 2024

C-style casts are necessary in explicit object member functions to access members of a potentially inaccessible base class (https://godbolt.org/z/Gb6vzzY9r). This C-style cast is load-bearing.

Thanks for your correction, I think I missed that, sorry.
Is there an equivalent (uncomplicated) C++ way of doing this?

@frederick-vs-ja
Copy link
Contributor

frederick-vs-ja commented Jul 11, 2024

Quoted from P2637R3:

The C-style cast here is deliberate because variant might be a private base of Self. This is a case that std::visit does not support, but LEWG preferred if member visit did.

Is there an equivalent (uncomplicated) C++ way of doing this?

I don't believe there's any, unfortunately. I think if there were, MSVC STL wouldn't drop uses of explicit object parameters in expected. See also microsoft/STL#2643 (comment).

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

3 participants