默认函数对象值作为要使用lambda调用的函数参数
default function object value as function parameter to be called with a lambda
我想要一个方法,该方法以默认值为参数的函数对象,用lambda函数调用,例如:
#include <iostream>
#include <functional>
void func(const std::function<void()>& f = {}){
if(f) f();
else std::cout << "or not" << std::endl;
}
int main() {
func([](){ std::cout << "hello" << std::endl; });
func();
}
但在Visual Studio 2012上,它不会编译(例如,它使用Visual Studio 2015或g++进行编译),抱怨默认值{}。将其更改为:
void func(const std::function<void()>& f = nullptr){
解决问题。1) 这是编译器不支持的功能吗?2) 两者之间有什么区别吗?
1)这是编译器不支持的功能吗?
您的测试显示情况确实如此。它是一个标准特性,因此不支持它意味着编译器不符合标准。
2)两者之间有什么区别吗?
没有区别。默认构造函数和采用nullptr_t
的构造函数的行为完全相同。
相关文章:
- 可组合的lambda/std::函数与std::可选
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何建立使用模板函数的lambda函数的尾部返回类型
- 如何将lambda作为模板类的成员函数参数
- 我可以将调用类的"this"传递给 lambda 函数吗?
- 模板函数指针和lambda
- 两组使用lambda函数的大括号
- 尝试将lambda函数放在队列中时出现一般分配器错误(可能是与unique_ptr有关的错误)
- 我可以在这里替换什么,因为我不能在 C# 中使用隐式变量的 lambda 函数?
- 在构造函数中使用 lambda 的 C++ 类
- 如何调用存储在指向"std::函数"的指针中的 lambda?
- 为什么我不能在 constexpr lambda 函数中使用 std::tuple
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- C++:Lambda 函数指针转换的用例是什么?
- 将有状态的 lambda 传递到 C 样式函数中,而无需上下文参数
- std::映射服装比较函数和函数/lambda错误
- 在可移动类型的构造函数 lambda 中捕获此内容的安全使用
- 排序测试模板化函数 lambda:非法使用此类型作为表达式
- 将匿名函数(lambda)保存为函数类型变量
- 如何编写将自身作为回调传递的匿名函数/lambda