我可以创建一个具有相同签名的String + lambda表的表吗? [英] Can I make a table of String + lambdas that have the same signature?

查看:93
本文介绍了我可以创建一个具有相同签名的String + lambda表的表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种模式:

  Thing * y = FindNearestItem 
if(y&&(MenuElement * x = FindMenuElementNamed(Identity)))
x-> SetText(FString(%。1f,y)
else if(x)
x-> Clear();
if(y&&(MenuElement * x = FindMenuElementNamed(X1)))
x-> SetLocalData(y);
else if(x)
x-> Clear();

基本上,我想使用一个静态表:
[警告: ,无效,我是一个noob,你已被警告]:

  struct Table {
const char * 标签;
?lambda? λ;
} MyTable [] = {
Identity,[](const char * label,Thing * y){MenuElement * x = FindMenuElementNamed(label); (y& x)? x-> SetText(FString(%。1f,y)):x-> Clear(); },
X1,[](const char * label,Thing * y){MenuElement * x = FindMenuElementNamed(label); (y& x)? x-> SetLocalData(y):x-> Clear(); },
};

Thing * y = FindNearestItem();
for(int i = 0; i!= countof(MyTable); ++ i)
MyTable [i] .lambda(MyTable [i] .label,y)

请注意,每个标签(表格中的每一行) p>

所以模式大部分是相同的,但方差是在采取的行动,虽然它在每种情况下使用相同的数据集(x,y,标签)。但我不能简单地调用x-> DoAppropriateThingFor(label,y);我只是回到创建一个长的if / else级联基于标签...



随时问我进一步澄清。因为我还没有机会真正使用它们,所以我在黑暗中混乱了...

解决方案

只要所有的lambda是无捕获的(即 [] 是空的),你可以使用一个函数指针:

  struct Table {
const char * label;
void(* lambda)(const char *,Thing *);
};



如果任何一个lambdas是有状态的(即 [] 不为空),那么你不能使用函数指针。您可以使用 std :: function

  
const char * label;
std :: function< void(const char *,Thing *)> λ;
};

Visual C ++ 2010不支持lambda-to-function-pointer转换Visual C ++ 2010发布后的语言),但Visual C ++ 11开发人员预览支持转换。如果使用Visual C ++ 2010,可以使用 std :: function 解决方案。


I have this pattern:

Thing * y = FindNearestItem();
if (y && (MenuElement * x = FindMenuElementNamed("Identity")))
  x->SetText(FString("%.1f", y));
else if (x)
  x->Clear();
if (y && (MenuElement * x = FindMenuElementNamed("X1")))
  x->SetLocalData(y);
else if (x)
  x->Clear();

Basically, I want to use a static table: [WARNING: really sloppy conceptual code, not valid, I'm a noob, you've been warned]:

struct Table {
  const char * label;
  ?lambda? lambda;
} MyTable[] = {
  "Identity", [] (const char * label, Thing * y) { MenuElement * x = FindMenuElementNamed(label); (y && x) ? x->SetText(FString("%.1f", y)) : x->Clear(); },
  "X1", [] (const char * label, Thing * y) { MenuElement * x = FindMenuElementNamed(label); (y && x) ? x->SetLocalData(y) : x->Clear(); },
};

Thing * y = FindNearestItem();
for (int i = 0; i != countof(MyTable); ++i)
  MyTable[i].lambda(MyTable[i].label, y);

PLEASE keep in mind that that the action differs for each label - each row in my table.

So the pattern is mostly the same, but the variance is in the action taken, though it uses the same set of data (x,y,label) in each case. But I cannot simply call x->DoAppropriateThingFor(label,y); I'd just be back to creating a long if/else cascade based on label...

Feel free to ask me for further clarification. I'm muddling around in the dark with lambdas since I've not had a chance to really use them yet...

解决方案

As long as all of the lambdas are captureless (i.e., the [] is empty), you can use a function pointer:

struct Table {
    const char* label;
    void (*lambda)(const char*, Thing*);
};

If any of the lambdas are stateful (i.e., the [] is not empty), then you can't use a function pointer. You can use std::function, though:

struct Table {
    const char* label;
    std::function<void(const char*, Thing*)> lambda;
};

Visual C++ 2010 does not support the lambda-to-function-pointer conversion (that conversion was added to the language after Visual C++ 2010 was released), but the Visual C++ 11 Developer Preview does support the conversion. If you are using Visual C++ 2010, you can use the std::function solution.

这篇关于我可以创建一个具有相同签名的String + lambda表的表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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