一个std :: pair的switch语句?

时间:2016-02-12 09:28:37

标签: c++11 switch-statement pattern-matching

我想切换两个整数的可能值,或者在另一种情况下切换两个bool。为了便于讨论,假设我已经完成了

auto mypair = std::make_pair(foo, bar);

如何实现

的等价物
switch(mypair) {
case make_pair(true, false): cout << "true and false"; break;
case make_pair(false, true)  cout << "false and true"; break;
case default: cout << "something else";
}

使用C ++ 11? (如果有帮助,C ++ 14/17也相关)?

2 个答案:

答案 0 :(得分:2)

C ++的switch语句没有许多其他语言的模式匹配能力。你需要采取略微不同的方法。

我有可能在一起:

pair_switch(my_pair,
    std::make_tuple(true, false, []{ std::cout << "true and false"; }),
    std::make_tuple(false, true, []{ std::cout << "false and true"; }));

您提供std::pair<bool,bool>和一组案例std::tuples,其中前两个元素与您传入的对匹配,第三个元素是调用该案例的函数。

实现有一些模板技巧,但应该非常有用:

template <typename... Ts>
void pair_switch(std::pair<bool,bool> pair, Ts&&... ts) {
    //A table for the cases
    std::array<std::function<void()>, 4> table {};

    //Fill in the cases
    (void)std::initializer_list<int> {
        (table[std::get<0>(ts)*2 + std::get<1>(ts)] = std::get<2>(ts), 0)...
    };

    //Get the function to call out of the table
    auto& func = table[pair.first*2 + pair.second];

    //If there is a function there, call it
    if (func) {
        func();   
    //Otherwise, throw an exception
    } else {
        throw std::runtime_error("No such case");   
    }
}

Live Demo

答案 1 :(得分:1)

您只能打开一个整数类型,但是如果您可以设计一个函数来将您的对(或任何复杂类型)映射到整数类型,则可以将其声明为constexpr(C ++ 11)表明它可以在编译时解决。然后它可以作为案例表达。

简单示例:

enum Action { peel, eat, juice };
enum Fruit { apple, orange, banana };

constexpr unsigned int switch_pair(Action a, Fruit f) {
   return (a << 16) + f;
}

然后像这样定义开关:

switch(switch_pair(mypair.first,mypair.second))
{
   case switch_pair(peel,apple):  std::cout << "Peeling an apple" << std::endl; break;
   case switch_pair(eat,apple):   std::cout << "Eating an apple" << std::endl; break;
   case switch_pair(juice,apple): std::cout << "Juicing an apple" << std::endl; break;
   default:
      throw std::runtime_error("We only have apples!");
}
相关问题