cplusplus / draft

C++ standards drafts

Home Page:http://www.open-std.org/jtc1/sc22/wg21/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[variant.visit] C-style casting in `visit` member

hewillk opened this issue · comments

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)?

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.

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?

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).