Yes. In this respect lambda expressions are no different from other expressions (like, say, 0
). But note that deduction is not used with defaulted parameters. In other words, if you declare
template<typename T>
void foo(T = 0);
then foo(0);
will call foo<int>
but foo()
is ill-formed. You'd need to call foo<int>()
explicitly. Since in your case you're using a lambda expression nobody can call foo
since the type of the expression (at the site of the default parameter) is unique. However you can do:
// perhaps hide in a detail namespace or some such
auto default_parameter = [](int x) { return x; };
template<
typename Functor = decltype(default_parameter)
>
void foo(Functor f = default_parameter);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…