c++ - what's wrong with declaring a variable inside if's condition? -
c++ - what's wrong with declaring a variable inside if's condition? -
perhaps getting rusty (have been writing in python recently).
why not compile?
if ( (int i=f()) == 0)
without ()
around int i=f()
another, much more reasonable error of i
not beingness boolean. that's why wanted parentheses in first place!
my guess using parentheses makes expression, , declaration statements not allowed in expression. so? , if yes, 1 of c++'s syntax quirks?
btw, trying this:
if ( (mymap::iterator = m.find(name)) != m.end()) homecoming it->second;
you can declare variable in if
statement in c++ restricted used direct initialization , needs convert boolean value:
if (int = f()) { ... }
c++ doesn't have described "declaration expression", i.e. [sub-] expressions declaring variable.
actually, looked clause in standard , both forms of initialization supported according 6.4 [stmt.select] paragraph 1:
... condition: look attribute-specifier-seqopt decl-specifier-seq declarator = initializer-clause attribute-specifier-seqopt decl-specifier-seq declarator braced-init-list ...
that is, possible write:
if (int i{f()}) { ... }
obviously, works in c++2011 because c++2003 doesn't have brace-initialization.
c++ syntax variable-declaration
Comments
Post a Comment