vendredi 27 novembre 2015

Cast a pair containing void pointers to a pair of pointers of other type

Discussion

I know that the proper way to cast a void pointer to a pointer of other type is to use static_cast. (e.g.,

void *vptr;
...
foo* fptr = static_cast<foo*>(vptr);

)

What I have though, is a pair of void pointers (i.e., std::pair<void*, void*>), which I want to cast it to a pair of pointers of other type (e.g., std::pair<foo*, bar*>).

The most naive way I can think of to achieve this is the following:

std::pair<void*, void*> vpr;
...
std::pair<foo*, bar*> fbpr = std::make_pair(static_cast<foo*>(vpr.first), static_cast<bar*>(vpr.second));

Or since C++11:

std::pair<void*, void*> vpr;
...
auto fbpr = std::make_pair(static_cast<foo*>(vpr.first), static_cast<bar*>(vpr.second));

Question

Is there a neater, more elegant or efficient way to do this casting?

Aucun commentaire:

Enregistrer un commentaire