C++ lambda 和内联嘶嘶声
C++ lambda and inlined fizzbuzz
Heoi,我看过一个C++演讲,有人做了一个lambda fizzbuzz实现。
这不是它!甚至没有接近它!我的问题是,为什么我不能使用ostream和
auto fizz = [](int& x, std::ostream& os) { x % 3 == 0 ? os << "fizz" : 0; };
auto buzz = [](int& x, std::ostream& os) { x % 5 == 0 ? os << "buzz" : 0; };
for (int i = 0; i != 100; ++i)
{
fizz(i, std::cout);
buzz(i, std::cout);
}
我的错误消息是:
E1776 function "std::basic_ostream<_Elem, _Traits>::basic_ostream(const std::basic_ostream<_Elem, _Traits>::_Myt &) [with _Elem=char, _Traits=std::char_traits<char>]" (declared at line 83 of "c:Program Files (x86)Microsoft Visual Studio2017CommunityVCToolsMSVC14.12.25827includeostream") cannot be referenced -- it is a deleted function 56
你的问题很清楚。由于std::ostream
和int
不属于同一类型,因此向三元运算符提供不相同的类型会产生错误。为了解决这个问题,你可能希望完全避免使用 else 子句,所以你的函数看起来像这样:
auto fizz = [](int& x, std::ostream& os) { if (x % 3 == 0) os << "fizz"; };
auto buzz = [](int& x, std::ostream& os) { if (x % 5 == 0) os << "buzz"; };
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 可组合的lambda/std::函数与std::可选
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何建立使用模板函数的lambda函数的尾部返回类型
- MacOS通过在莫哈韦"wchar.h"下破碎的自制啤酒发出叮当声
- 如何将lambda作为模板类的成员函数参数
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 在 lambda 捕获中声明的变量的类型推导
- 我可以将调用类的"this"传递给 lambda 函数吗?
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- 模板函数指针和lambda
- 两组使用lambda函数的大括号
- 使lambda不可复制/不可移动
- FLTK:按下哪个按钮 - 将数字传递给按钮的回调 (lambda)
- 尝试将lambda函数放在队列中时出现一般分配器错误(可能是与unique_ptr有关的错误)
- 将带有unique_ptr的可变 lambda 传递给 const&std::function
- AWS Lambda C++运行时权限被拒绝
- 捕获lambda中的std::数组
- C++ lambda 和内联嘶嘶声
- Objective-C, CoreAudio:播放的声音有额外的噪音,嘶嘶声和砰的一声的可能原因