jeudi 24 août 2017

Does this method of getting last element in template parameter pack have hidden overhead?

I have a set functions with different arguments, but all of them have last element the same. So I need to access that last argument in my function. Looking around I found quite some solutions with template specializations (here and here), but this solution came to my mind:

template<typename ...Args>
void function( Args&&... args )
{
     auto &last = std::get<sizeof...( Args ) - 1 >( std::tie( args... ) );
}

It may look obvious, but it was not so for me in the begining. It seems to be simple and short, but does this method have any hidden overhead versus classic solutions with template specialization and helper functions/classes?

Aucun commentaire:

Enregistrer un commentaire