Can we use a lambda-expression as the default value for a function argument?(我们可以使用 lambda 表达式作为函数参数的默认值吗?)
问题描述
参考 C++11 规范 (5.1.2.13):
Refering to the C++11 specification (5.1.2.13):
出现在默认参数中的 lambda 表达式不应隐式或显式捕获任何实体.
[ 示例:
A lambda-expression appearing in a default argument shall not implicitly or explicitly capture any entity.
[ Example:
void f2() {
int i = 1;
void g1(int = ([i]{ return i; })()); // ill-formed
void g2(int = ([i]{ return 0; })()); // ill-formed
void g3(int = ([=]{ return i; })()); // ill-formed
void g4(int = ([=]{ return 0; })()); // OK
void g5(int = ([]{ return sizeof i; })()); // OK
}
——结束示例 ]
但是,我们是否也可以使用 lambda 表达式本身作为函数参数的默认值?
However, can we also use a lambda-expression itself as the default value for a function argument?
例如
template<typename functor>
void foo(functor const& f = [](int x){ return x; })
{
}
推荐答案
是的.在这方面,lambda 表达式与其他表达式(例如 0)没有区别.但请注意,默认参数不使用推导.换句话说,如果你声明
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); 将调用 foo 但 foo() 是格式错误的.您需要显式调用 foo .由于在您的情况下您使用的是 lambda 表达式,因此没有人可以调用 foo ,因为表达式的类型(在默认参数的位置)是唯一的.但是你可以这样做:
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);
这篇关于我们可以使用 lambda 表达式作为函数参数的默认值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我们可以使用 lambda 表达式作为函数参数的默认值吗?
基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
