Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Call a function using expansion pack on a same function

I would like to be able to call a function over any number of arguments, with each argument’s value supplied by another function:

template <typename T, std::size_t N>
class MyClass
{
public:
    void doSomething()
    {
        doSomethingProxy(std::make_index_sequence<N>());
    }

private:
    template <std::size_t... I>
    void doSomethingProxy(std::index_sequence<I...>)
    {
        m_obj.doSomething({get()...}); // T::doSomething() expects an object whose constructor has N number of arguments, and I would like to call the get() function for each argument of said constructor, N times
    }
    
    int get() { return 0; }

    T m_obj{};
};

However doing the above results in the error

error: expansion pattern get() contains no parameter packs

I think the issue is that get()... does not depend on I, but I’m not sure how to solve this problem.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

(I,get()) is using the parameter pack and results in get() (comma operator, evaluates both operands, returns the rhs):

#include <cstdint>
#include <utility>

template <typename T, std::size_t N>
class MyClass
{
public:
    void doSomething()
    {
        doSomethingProxy(std::make_index_sequence<N>());
    }

private:
    template <std::size_t... I>
    void doSomethingProxy(std::index_sequence<I...>)
    {
        m_obj.doSomething({ (I,get())... }); 
    }
    
    int get() { return 0; }

    T m_obj{};
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading