10
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++でもコンパイル時にa == 1 && a == 2 && a == 3をtrueにしてみたい

Last updated at Posted at 2018-01-22

(a==1&&a==2&&a==3)を常にtrueにするStackOverflowの問題を解いたQiita記事まとめ

とかいうよくわからん記事がでている。流行りに乗ってみるか。

独自の縛り

  • コンパイル時に評価
  • C++11

コード

まあコンパイル時と言ってもただのoperator overloadだ。

struct A { bool a; };
constexpr bool operator==(A, int) noexcept { return true; }
constexpr A a{};
static_assert(a == 1 && a == 2 && a == 3, "");
int main(){}

他の言語しか知らない人のために解説しておくと、static_assertは第1引数が偽のとき、第2引数の文字列を含むコンパイルエラーを出すものだ。エラーが出なければtrueになったといえる。

License

CC BY 4.0

CC-BY icon.svg

10
3
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
10
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?