7
6

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++][小ネタ]if文の条件式内で宣言した変数のスコープ

Last updated at Posted at 2016-06-12

for文でfor(int i=0;i<N;i++){...}とできるのは有名だが。if文の条件式でも、変数を宣言することができる。そして、そのスコープは、ifに対応するelseがある場合には、elseに続く文の最後までである。

よって、以下は正しいC++コードである。(int main(){ return 0; }と等価)

int main(){
	if(int i=0){}
	else if(i){}
	else return i;
}

非常に面白いが、知らない人が読むとなんじゃこりゃってなりそうだし、使い道もあんまり多くない。

7
6
2

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
7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?