variadic template use?

Hi there,

I started to make a program using the factory pattern. The way it work it's we have the template function construct which allocate a pointer and return a reference to the value. We have the possibility to send a Container class to add the pointer in the list inside the container before returning the reference. The container is a template class. The goal is to add constructed object to each object list it is inherited. Here's a sample of code:

1
2
3
4
5
6
7
        template<class T, class S>
        static T& construct(Container<S>& c)
        {
            T* ptr = new T;
            c.addData(ptr);
            return *ptr;
        }


But waht I wan tot do is something like that:

1
2
3
4
5
6
7
8
9
        template<class T, class ...S>
        static T& construct(Container<S>& ...c)
        {
            T* ptr = new T;

            //???

            return *ptr;
        }

What can I put to make this code working, I am searching for a while and it's very blocking .

Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
template <class T, class S>
void _addData(T* ptr, Container<S>& c)
{
        c.addData(ptr);
}
 
template <class T, class S, class... D>
void _addData(T* ptr, Container<S>& c, Container<D>&... Args)
{
        c.addData(ptr);
        _addData(ptr, Args...);
}
 
template <class T, class... S>
static T& construct(Container<S>&... Args)
{
        T* ptr = new T;
        _addData(ptr, Args...);
        return *ptr;
}


Then you can just call with construct<MyClass>(c1, c2, c3, c4, c5)
Last edited on
Thank you very much for your help.
Np, and thanks for the question. It gave me a reason to go read about variadic templates :D
Topic archived. No new replies allowed.