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

How to define function argument as an other function argument

For example how to impalement Wrapper::call_to_func
that argument list call_to_func should be as func

class A
{
    void func(int, char, double);
};

template<class T>
class Wrapper
{
public:
    void call_to_func(....)
    {
          m_t.func(....)
    }

    T m_t;
}

>Solution :

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

You can make call_to_func a variadic method template with a template parameter pack , and use std::forward to perfectly forward the arguments to func:

#include <iostream>
#include <utility>

class A {
public:
    void func(int i, char c, double d) {
        std::cout << "func" << " " << i << " " << c << " " << d << "\n";
    }
};

template<class T>
class Wrapper {
public:
    template <typename ... Ts>
    void call_to_func(Ts&& ... args) {
        m_t.func(std::forward<Ts>(args)...);
    }

    T m_t;
};

int main() { 
    Wrapper<A> w;
    w.call_to_func(1, 'a', 2.3);
}

Output:

func 1 a 2.3

Live demo

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