LoginSignup
6
5

More than 5 years have passed since last update.

C++ 演算子の代替表現

Posted at

C++には演算子の代替表現というものがあります。それはすなわち、以下のようなコードが書けるという意味です。

if (n not_eq m)
{
  std::cout << "n is not equal to m." << std::endl;
}
if (n and m)
{
  std::cout << "both n and m are not false." << std::endl;
}

今日知ったのですが、この代替表現は文脈を問わず使うことができます。

class sth
{
public:
  void func(int and n) and
  {
    std::cout << n << " and" << std::endl;
  }
  void func(int bitand n) bitand
  {
    std::cout << n << " bitand" << std::endl;
  }
};

妙な気分ですが、このコードは合法です。以下のように使うこともできます。

int main()
{
  int a = 123;
  sth().func(static_cast<int and>(a));
  sth s;
  s.func(* bitand a);
}

いやあ、知らなかった。一生使うことはなさそうです。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5