Search code examples
c++templatesvariadic-templatesvariadictype-deduction

C++ template pack deduction - What am I doing wrong in this example?


This compiles and works fine:

template<typename T, typename ... Args>
void func()
{
    std::cout << __PRETTY_FUNCTION__ << std::endl;
    func<Args...>();
}

int main()
{
    func<int, char, float>();
    return 0;
}

...and this also compiles and works fine:

struct Object {};   // Some dummy object

template<typename T, typename ... Args>
void func(Object *a)
{
    std::cout << __PRETTY_FUNCTION__ << std::endl;
    func<Args...>(a);
}

int main()
{
    Object *a = new Object;
    func<int, char, float>(a);
    return 0;
}

...but this does NOT compile, because it cannot resolve T:

struct Object {};   // Some dummy object

template<typename T, typename ... Args>
void func(Object *a, Object *b)
{
    std::cout << __PRETTY_FUNCTION__ << std::endl;
    func<Args...>(a, b);
}

int main()
{
    Object *a = new Object;
    Object *b = new Object;
    func<int, char, float>(a, b);
    return 0;
}

What am I doing wrong here? Sorry for not being able to put on Ideone, blocked from work.


Solution

  • As pointed out by @NathanOliver, your code does not compile neither using clang nor using gcc.

    The following functions get instantiated:

    func<int, char, float>()
    func<char, float>()
    func<float>()
    

    Now, the parameter pack is empty, and the compiler tries to instantiate func<> which leads to the error that the template argument T cannot be deduced.

    In order to end the recursion properly, you can use one of the answers of this SO question, e.g.:

    #include <iostream>
    
    template <typename Last>
    void func()
    {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
    
    template<typename First, typename Second, typename ... Args>
    void func()
    {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
        func<Second, Args...>();
    }
    
    int main()
    {
        func<int, char, float>();
        return 0;
    }
    

    live example

    output

    void func() [First = int, Second = char, Args = <float>]
    void func() [First = char, Second = float, Args = <>]
    void func() [Last = float]