0

The code below will not compile, because when I want to specify Param, type deduction of A and B is disabled and I would have to specify those explicitly as well.

#include <iostream>

template<typename A, typename B>
struct L
{
};

template<typename A, typename B, unsigned Param = 0>
void fn(L<A, B> l_) {}

int main()
{ 
    L<int, float> l;
    fn<3>(l);
}

Coliru

Is it possible to have the best of both worlds with deduction of A and B and yet still be able to provide Param, which has a default value in case it is left out?

Post Self
  • 1,471
  • 2
  • 14
  • 34

1 Answers1

4

Yes of course, you just need to put the Param argument first (so that you can explicitly specify it):

template<unsigned Param = 0, typename A, typename B>
void fn(L<A, B> l_) {}

Note that this works even though A and B don't have default parameters because they can be deduced.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162