This is a follow up of my last post. I have successfully been able to set and get values of the classes. However, now I am trying to take this a step further and have the constructor take itself as the parameter. However, I am not sure how to properly unpack it.
I have tried:
#ifndef CONTROLLER_HPP
#define CONTROLLER_HPP
#include <functional>
#include <vector>
#include <iostream>
class Controller
{
public:
template <class...Classes>
Controller(Classes & ...classes)
{
toSet = [&](int val){(classes.updateValue(val), ...); };
toGet = [&](std::vector<int> &values)
{
int size[sizeof...(Classes)] = { (classes.get())...};
for(const auto &e: size) values.push_back(e);
};
}
// TODO: Find out how to create one master group if more than one are created.
//template <Controller&...Controllers, class...Classes>
template <class...Classes>
Controller(Controller& controllers(Classes&...classes)...) : Controller(classes&...c){};
void setValues(int val)
{
toSet(val);
}
std::vector<int> getValues()
{
std::vector<int> values;
toGet(values);
return values;
}
private:
std::function<void(int)> toSet;
std::function<void(std::vector<int>&)> toGet;
};
#endif
However, in this case, I get classes was not declared in this scope
error when I try to pass it to the intial controller constructor. I have also tried the commented out template declaration, however Id on't think that is correct either. I have also tried Controller&...controllers(Classes&...)...) : (Controller(Classes&...classes));
, but that doesn't work either.
I don't really know what to try next or if what I am asking is possible to do. Or maybe this is easier if I templatize the entire class. I was simply trying to avoid Controller<A,B> controller(A,B);
and instead just create Controller controller(A,B)
. However, I understand if I have to do it the other way.
EDIT: I should clarify what I am trying to do:
int main()
{
ClassA A;
ClassB B;
ClassC C;
ClassD D;
Controller controller1(A,B);
Controller controller2(C,D);
Controller master(controller1,controller2);
master.setValues(20);
std::vector<int> getVals = master.getValues();
for(const auto& e: getVales) std::cout << e << " ";
}
This would then set all the values of all the classes and get the values of all the classes within the controllers.