Initializer list in user-defined literal parameter

前端 未结 3 1266
陌清茗
陌清茗 2021-01-12 02:16

I don\'t know if it\'s possible but I want to do stuff like

int someval = 1;
if({1,2,3,4}_v.contains(someval ))

but when I try to define l

3条回答
  •  天涯浪人
    2021-01-12 02:58

    How about this:

    #include 
    
    template 
    bool contains(std::initializer_list const & il, T const & x)
    {
        for (auto const & z : il) { if (z == x) return true; }
        return false;
    }
    

    Usage:

    bool b = contains({1, 2, 3}, 5);  // false
    

提交回复
热议问题