How does std::visit work with std::variant?

孤者浪人 提交于 2019-11-30 12:52:16

What I think, is that under the hood std::visit builds an array of function pointers (at compile time) which consists of instantiated function pointers for each type. The variant stores a run-time type index i (intgeral number) which makes it possible to select the right i-th function pointer and inject the value.

You might wonder how can we store function pointers with different argument types in a compile time array? -> This is done with type-erasure (I think), which means one stores functions with e.g. a void* argument, e.g. &A<T>::call:

template<typename T>
static A
{
   static call(void*p) { otherFunction(static_cast<T*>(p)); } 
}

where each call dispatches to the correct function otherFunction with the argument (this is your lambda at the end). Type erasure means the function auto f = &A<T>::call has no more notion of the type T and has signature void(*)(void*).

std::variant is really complicated and quite sophisticated as lots of powerful fancy metaprogramming tricks come into play. This answer might only cover the tip of the iceberg :-)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!