jeudi 27 août 2020

Variadic function that accepts arguments of same type at compile time and iterate on them

I am looking for a way to implement Variadic function that accepts arguments of same type at compile time and should be able to iterate on them. Something like below -

 void SampleFunc(int... arg)
    {
           for(const auto& val : arg)
           {
               // Each argument available here.
           }
    }

then I will call this function like below -

SampleFunc({1,2,3,4})

Most important thing is that parameters are hard coded every time the function is called so I should be able to generate this Variadic arguments at compile time.

Right now I am accepting function parameters as shown below -

 void SampleFunc(std::vector<int>& nums)

But this adds run time cost of constructing a vector every time function is called which I want to avoid. Thanks a tone in advance!!!

Aucun commentaire:

Enregistrer un commentaire