Topic: is variable declaration in if statement conditional expression


Author: Brendan<catphive@catphive.net>
Date: Tue, 29 May 2012 11:25:02 -0700 (PDT)
Raw View
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?

Thanks,
Brendan Miller


--
[ 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                      ]