From 732081030347765289
X-Google-Thread: f78e5,4b488ac9fde12d7
X-Google-NewGroupId: yes
X-Google-Attributes: gid7894ca11fe,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII
Received: by 10.68.223.40 with SMTP id qr8mr11681848pbc.0.1338355198802;
        Tue, 29 May 2012 22:19:58 -0700 (PDT)
MIME-Version: 1.0
Path: pr3ni64817pbb.0!nntp.google.com!news1.google.com!goblin1!goblin.stu.neva.ru!news.albasani.net!.POSTED!not-for-mail
From: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Newsgroups: comp.std.c++
Subject: Re: is variable declaration in if statement conditional
 expression  allowed by standard?
Date: Tue, 29 May 2012 22:19:56 -0700 (PDT)
Organization: A noiseless patient Spider
Lines: 41
Sender: std-cpp-request@vandevoorde.com
Approved: james.dennett@gmail.com
Message-ID: <jq3604$fj6$1@dont-email.me>
References: <8629f8c3-a126-4b80-b475-8f9b049dde49@vy7g2000pbc.googlegroups.com>
NNTP-Posting-Host: qlvlrCivTRdScs1belzTYxwlYM3ujIBVd47xaviTaZE=
X-Trace: news.albasani.net b+A4qPXHMKuNBMtpyNYkt66GXFTnGzUi6ocIa5VdtE2bOZ5l7K2dK2mn6PFAaeJdTTa3QbtKKL8c/HAR24jOcQ==
X-Complaints-To: abuse@albasani.net
NNTP-Posting-Date: Wed, 30 May 2012 05:19:58 +0000 (UTC)
X-Mailer: Perl5 Mail::Internet v2.05
X-Submission-Address: std-cpp-submit@vandevoorde.com
Cancel-Lock: sha1:1Mg+R8a31cgQ8P9GLJTm6JXl5dg=
X-Original-Date: Tue, 29 May 2012 20:54:01 +0200
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Am 29.05.2012 20:25, schrieb Brendan:
>
> I didn't think it was, but recently I wrote this and was surprised
> when it actually worked on my compiler (G++):
>
> if (int err_code = some_function()) {
>      throw std::runtime_error(strerror(err_code));
> }
>
> Testing is seemed that err_code was scoped to the if statement in the
> same way it would have been in a for loop.
>
> Is this actually allowed by the standard, or is this just a GNU
> extension?


This is no extension, but valid C++03 and C++11. The grammar of the
if-selection statement used here is:

if ( condition ) statement

and /condition/ is defined as:

condition:
 expression
 type-specifier-seq declarator = assignment-expression

The second item corresponds to your example. The wording also
guarantees that the name introduced here (err_code) is local to the
if-statement.

HTH & Greetings from Bremen,

Daniel Kr�gler


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]


