Initializer list in user-defined literal parameter

前端 未结 3 1298
陌清茗
陌清茗 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 03:00

    you'd expect syntax to be

    if value in (value1, value2 ...) 
    

    or something similar.

    If you're willing to add one extra character, try this syntax:

    #include 
    #include 
    #include 
    
    template 
    bool operator *(const T0& lhs, const std::array& rhs) {
      return std::find(begin(rhs), end(rhs), lhs) != end(rhs);
    }
    
    template std::array in(T0 arg0, T...args) {
      return {{arg0, args...}};
    }
    
    int main () {
      if( 2 *in(1,2,3) ) { std::cout << "Hello\n"; }
      if( 4 *in(5,6,7,8) ) { std::cout << "Goodbye\n"; }
    }
    

提交回复
热议问题