C++ “OR” operator

后端 未结 9 1595
无人及你
无人及你 2021-01-19 03:37

can this be done somehow?

if((a || b) == 0) return 1;
return 0;

so its like...if a OR b equals zero, then...but it is not working for me.

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-19 03:46

    Fun with templates:

    template 
    struct or_t
    {
      or_t(const T& a, const T& b) : value1(a), value2(b)
      {
      }
    
      bool operator==(const T& c)
      {
        return value1 == c || value2 == c;
      }
    
    private:
      const T& value1;
      const T& value2;
    };
    
    template 
    or_t or(const T& a, const T& b)
    {
      return or_t(a, b);
    }
    

    In use:

    int main(int argc, char** argv)
    {
      int a = 7;
      int b = 9;
    
      if (or(a, b) == 7)
      {
      }
    
      return 0;
    }
    

    It performs the same comparison you would normally do, though, but at your convenience.

提交回复
热议问题