mardi 27 août 2019

How to disambiguate overloaded template functions

There is a problem with the following code. While 1 part is OK, problem is with the 2nd part of the main(). On compilation an ambiguous error message is displayed. How can I change the code to resolve the ambiguity?

template<typename Arg> void func(Arg arg) 
{  
    arg();
}
template<typename Arg, typename... Args> void func(Arg arg, Args... args) 
{  
    func(args...);
    arg();
}
template<typename Container> void func(Container & c) 
{
    for (typename Container::reverse_iterator i = c.rbegin(); i != c.rend(); ++i ) 
    { 
        (*i)();
    } 
}

void f()
{
    std::cout << "+" ;
}

int main()
{
    //1
    func(f,f,f);

    //2    
    std::vector<std::function<void()> > v{f,f};
    func(v);
}

Link to code: cpp.sh/3wxrc

Aucun commentaire:

Enregistrer un commentaire