I have this code:
#include <tuple>
#include <memory>
template <typename ...Args>
class Button
{
Button(const std::string& name, Args&& ...args) {
std::tuple<Args...> tuple_ = std::tuple<Args...>(args...);
}
};
template<typename T1, typename ...Args>
void addObject(const std::string& name, Args&& ...args) {
std::unique_ptr<T1> obj(new T1(name, std::forward<Args>(args)...));
//...rest of the code...
}
int main() {
//if we comment the next line it compiles well
addObject<Button>("BtnName", 0, 1, 2);
return 0;
}
However, it is not compiling with errors like "error LNK2019: unresolved external symbol..." or "no matching function for call to 'addObject class Button>(const char [8], int, int, int)'". If we comment "addObject" function it compiles well.
How to pass args to another template in the right way?
Button
is a class template; when using it in addObject<Button>("BtnName", 0, 1, 2);
, you have to specify template arguments for it like Button<...some arguments...>
.
Besides the solution above, maybe you want to move template parameter Args
from class to the constructor of Button
, then it could be deduced from the function arguments (i.e. std::forward<Args>(args)...
).
class Button
{
public:
template <typename ...Args>
Button(const std::string& name, Args&& ...args) {
std::tuple<Args...> tuple_ = std::tuple<Args...>(std::forward<Args>(args)...);
}
};
Other issues:
Button
is private
.#include <string>
.