mercredi 1 avril 2020

How to pass variadic parameters from one template to another

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?

Aucun commentaire:

Enregistrer un commentaire