Specialization of variadic template function over the non-variadic arguments

Viewed 119

I have a template for a function that accepts at least one parameter and performs some formatting on the rest:

template <typename T, typename... ARGS>
void foo(T first, ARGS&&... args)
{
    // ...
}

I want it to do a different thing when the first parameter is of a specific type. I like the compiler to choose this particular version, where the rest of the variadic arguments are just ignored. So I tried template specialization:

template <>
void foo(const ICON* p)
{
    // ...
}

or:

template <typename T, typename... ARGS>
void foo(const ICON* p, ARGS&&... args)
{
   // ...
}

I also tried a non-templated overload, though it always picks the first version.

The caller code is supposed not to know about templates at all (legacy code, backwards-compatibility nonsense, etc.) So it relies on type deduction and calls like this:

MyClass fooMaker;
fooMaker.set("A", "B", 3.14, 42); // v1
fooMaker.set(pIcon);              // v2 (special)
2 Answers

If you have access to or later, using if constexpr you can retain the true statement in the foo, at compile time, as follows:

#include <type_traits> // std::is_pointer_v, std::is_same_v

template <typename T, typename... ARGS>
void foo(T first, ARGS&&... args)
{
    if constexpr (std::is_pointer_v<T> // is pointer check
        && std::is_same_v<T, ICON*>)   // is the type is of "ICON*"
    {
        // ICON impli...
    }
    else
    {
        // other impli...
    }
}

See online demo

Since function templates can't be partially specialized, we can overload them as shown below to get the desired effect:

struct ICON{};
template <typename T, typename... ARGS>
void foo(T first, ARGS&&... args) // #1
{
    std::cout<<"primary version selected"<<std::endl;
}
//-------v--------------------->typename T removed from here
template<typename... ARGS>
void foo(ICON*, ARGS&&...args)// #2
{
    std::cout<<"overloaded version used"<<std::endl;
}
int main()
{
    foo(1,2, "some string literal"); //calls #1
    ICON i{};
    ICON *ptrI = &i;
    foo(ptrI);                       //calls #2                     
    
    return 0;
}

Working demo

Related