if-else中的C ++ 11 lambda函数定义 [英] C++11 lambda function definition in if-else

查看:91
本文介绍了if-else中的C ++ 11 lambda函数定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何做,但要采用可以编译的方式,并希望没有疯狂的typedef,该怎么做?

How do I do something like this, but in a way that will compile, and hopefully without insane typedefs?

auto b;
auto g;
if (vertical)
{
    b = [=, &b_](int x, int y) -> bool { return b_[x + y*w]; };
    g = [=, &g_](int x, int y) -> int& { return g_[x + y*w]; };
}
else
{
    b = [=, &b_](int x, int y) -> bool { return b_[y + x*w]; };
    g = [=, &g_](int x, int y) -> int& { return g_[y + x*w]; };
}

推荐答案

auto b;auto g;将需要初始化,以便编译器能够确定其类型(但我想您知道这一点).

The auto b; and auto g; would need to be initialised so that the compiler would be able to determined their type (but I think you know this).

由于您似乎不介意类型的细节(无论如何您都声明了auto);

Since you don't seem to mind the specifics of type (you've declared them auto anyway);

std::function<bool(int,int)> b;
std::function<int&(int,int)> g;

应该做到这一点.

这篇关于if-else中的C ++ 11 lambda函数定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆