I have a class in which I take any number of classes and store them into a std::tuple
. It is a variadic templated class. I have properly overloaded the operator+
operator and they work as intended. However, now I am trying to assign one controller class to another. I have tried the following:
template<typename...ClassesR>
auto operator=(const Controller<ClassesR...>& rhs)
{
objects.swap(rhs.getObjects());
return *this;
}
However, when I compile the code, I get the error: "No matching function for call to std::tuple<A&,B&>::swap(std::tuple<A&,B&,A&>)
note: no known conversion for argument 1 from std::tuple<A&,B&,A&>
to std::tuple<A&,B&>&
"
Simply put, I am trying to do the following:
ClassA A(3);
ClassB B(3.4);
ClassC C(4.5f);
Controller<ClassA,ClassB> controllerA(A,B);
Controller<ClassC> controllerB(C);
// thinking about it now, this might be the problem...because controllerA is actually
// Controller<ClassA,ClassB> and not Controller<ClassA,ClassB,ClassA>.
controllerA = controllerA + controllerB;
//or
//controllerA = controllerB;
Here is the code that I am working with:
#ifndef CONTROLLER_HPP
#define CONTROLLER_HPP
#include <functional>
#include <vector>
#include <utility>
#include <any>
template<typename...Classes>
class Controller
{
public:
Controller(Classes&...objects) : objects(objects...){ }
Controller(std::tuple<Classes&...> tup) : objects(tup){ }
//...a bunch of code that doesn't matter
std::tuple<Classes&...> getObjects() const
{
return objects;
}
template<typename...ClassesR>
auto operator=(const Controller<ClassesR...>& rhs)
{
objects.swap(rhs.getObjects());
return *this;
}
private:
std::tuple<Classes&...> objects;
};
template<typename...ClassesL, typename...ClassesR>
auto operator+(const Controller<ClassesL...>& lhs, const Controller<ClassesR...>& rhs)
{
return Controller(std::tuple_cat(lhs.getObjects(),rhs.getObjects()));
}
template<typename...ClassesL, typename ClassesR>
auto operator+(const Controller<ClassesL...> &lhs, ClassesR rhs)
{
Controller<ClassesR> makeController(rhs);
return Controller(std::tuple_cat(lhs.getObjects(),makeController.getObjects()));
}
template<typename ClassesL, typename...ClassesR>
auto operator+(ClassesL lhs, const Controller<ClassesR...> &rhs)
{
Controller<ClassesL> makeController(lhs);
return Controller(std::tuple_cat(makeController.getObjects(),rhs.getObjects()));
}
#endif // CONTROLLER_HPP
What is the proper way to overload operator=
in this case? As I noted, as I am writing this it is possible that it is because the templated classes are probably set in stone. So Controller<ClassA,ClassB>
cannot be modified to Controller<ClassA,ClassB,ClassA>
so maybe I need to return a new controller?