Search code examples
c++templatesparameter-passingtemplate-argument-deductiondefault-arguments

Way to Only Pass Second Template Parameter


So I have a template function which has a defaulted 2nd argument. It's 1st argument can be deduced, so something like:

template <typename F, typename S = int>
void foo(const F param)

This works fine in the general case, I'll just call foo(bar). But in the case where I want to specify the second argument, I cannot do this: foo<char>(bar), because char is taken as F. Obviously this isn't the case because bar isn't a char, so F should be deducible.

Is there a way that I can pass only one template argument here which will apply to S, still have F be deduced, and still default S in the general case?


Solution

  • Just reorder template arguments:

    template <typename S = int, typename F>
    void foo(const F param);
    

    template can have default at any place. At the end, all template parameters should be provided, defaulted or deduced.