更好的方式说x == Foo :: A || x == Foo :: B || x == Foo :: C || ...? [英] Better way to say x == Foo::A || x == Foo::B || x == Foo::C || ...?

查看:170
本文介绍了更好的方式说x == Foo :: A || x == Foo :: B || x == Foo :: C || ...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些众所周知的值,像这样(但 const char * 只是一个例子,它可能更复杂):

  const char * A =A,* B =B,* C =C,* D =D ,* E =E,* F =F,* G =G 

现在让我们说,如果某个表达式的结果是一个子集

  if(some_complicated_expression_with_ugly_return_type == A || 
some_complicated_expression_with_ugly_return_type == C ||
some_complicated_expression_with_ugly_return_type == E ||
some_complicated_expression_with_ugly_return_type == G)
{
...
}

我发现自己经常输入这样的东西,我想要一个简写。



如果语言是Python,我可以很容易地说:

 如果some_complicated_expression_with_ugly_return_type在[A,C,E,G]:
...

有没有一个众所周知的,可移植的方式让我在C ++ 03中类似地表达? / p>

请注意,返回类型本身是丑陋的(几乎像 lambda表达式),所以我当然不想将它存储在一个局部变量。



但是返回类型必须与常量匹配 - 例如,如果返回类型 std :: string ,它将不能隐式转换为 const char * ,但



到目前为止,我最好的解决办法是说: p>

  const char * items [] = {A,C,E,G}; 
if(std :: find(items,items + sizeof(items)/ sizeof(* items),
some_complicated_expression_with_ugly_return_type)
!= items + sizeof )
{
...
}

darn丑陋。

解决方案

您可以将当前最佳解决方案考虑到模板中:

 模板< class A,class B,size_t n> 
inline bool is_in(const A& a,B(& bs)[n]){
return std :: find(bs,bs + n,a)!= bs + n;
}

您可以使用

  X items [] = {A,C,E,G}; 
if(is_in(some_complicated_expression_with_ugly_return_type,items))
...


Let's say I have a bunch of well-known values, like this (but const char * is just an example, it could be more complicated):

const char *A = "A", *B = "B", *C = "C", *D = "D", *E = "E", *F = "F", *G = "G";

Now let's say I want to behave in a particular way if the result of some expression is in a subset of those:

if (some_complicated_expression_with_ugly_return_type == A ||
    some_complicated_expression_with_ugly_return_type == C ||
    some_complicated_expression_with_ugly_return_type == E ||
    some_complicated_expression_with_ugly_return_type == G)
{
    ...
}

I find myself typing this sort of thing often enough that I would like a shorthand for it.

If the language was Python, I could easily say:

if some_complicated_expression_with_ugly_return_type in [A, C, E, G]:
    ...

Is there a well-known, portable way for me to express this similarly in C++03?

Note that the return type is itself ugly (almost as ugly as the return type of lambda expressions), so I certainly don't want to store it in a local variable.

But the return type does not have to match that of the constants -- for example, if the return type was std::string, it would not be implicitly convertible to const char *, but operator == would be perfectly fine for the comparison.

So far, the best solution I have is to say something like:

const char *items[] = { A, C, E, G };
if (std::find(items, items + sizeof(items) / sizeof(*items),
              some_complicated_expression_with_ugly_return_type)
    != items + sizeof(items) / sizeof(*items))
{
    ...
}

but it's pretty darn ugly. Is there a better way, which also works for non-PODs?

解决方案

You could factor your current best solution into a template:

template<class A, class B, size_t n>
inline bool is_in(const A &a, B (&bs)[n]) {
  return std::find(bs, bs + n, a) != bs + n;
}

which you can use like

X items[] = { A, C, E, G };
if (is_in(some_complicated_expression_with_ugly_return_type, items))
  …

这篇关于更好的方式说x == Foo :: A || x == Foo :: B || x == Foo :: C || ...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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