Hacker News new | past | comments | ask | show | jobs | submit login

AFAIK the usual implementation is statically generating an array of function pointers and dispatching based on the type discriminator. This works even less for inlining.



On my phone so cannot check, but I thought this was mostly a solved problem? Inlining/folding a jump table should be easy enough especially since the table can be const


https://godbolt.org/z/d4838K

Doesn't happen, apparently.

edit: For a short while libc++ adopted mpark's variant visit, which AFAIK compiles down to switch case with some hand coding. AFAIK it had other problems so they rolled back to the constexpr array of function pointers.

https://github.com/mpark/variant


If you call foo with an int or with a double, then it gets inlined.

I don’t think they can inline foo as you’ve written it because the variant could be valueless by exception.


I just looked into this a bit more. For the simple examples I tried, clang and gcc inline + const prop calls to std::visit with O2.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: