Discussion:
If statement with initializer
(too old to reply)
Thiago Adams
2017-05-10 13:29:33 UTC
Permalink
Raw Message
C++ 17, has now a new if.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html

if ( for-init-statement condition ) statement

is equivalent to

{
for-init-statement
if ( condition ) statement
}

and

if ( for-init-statement condition ) statement else statement

is equivalent to

{
for-init-statement
if ( condition ) statement else statement
}

Would you like to see this if on C?
David Brown
2017-05-10 13:42:22 UTC
Permalink
Raw Message
Post by Thiago Adams
C++ 17, has now a new if.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html
if ( for-init-statement condition ) statement
is equivalent to
{
for-init-statement
if ( condition ) statement
}
and
if ( for-init-statement condition ) statement else statement
is equivalent to
{
for-init-statement
if ( condition ) statement else statement
}
Would you like to see this if on C?
I would rather see it in a manner that does not need the extra clause -
or at least, the extra clause should be optional.

In C++17, you can now write:

if (bool b = foo(); b) ...

as equivalent to:

{
bool b = foo();
if (b) ...
}

(Note the critical extra set of brackets.)

I think it would have been much nice if it had been simply:

if (bool b = foo()) ...


I would not mind if this came to C, but I think it is unlikely. The
change is useful in C++ because you sometimes want precise control of
when a variable goes out of scope and lifetime, and therefore gets
destroyed. In C, nothing special happens when a local variable goes out
of scope, so it matters little if its scope and lifetime is extended.
At most, it would be a syntactic convenience allowing you to re-use the
same identifier without using new sets of brackets.
Ben Bacarisse
2017-05-10 14:36:25 UTC
Permalink
Raw Message
David Brown <***@hesbynett.no> writes:
<snip>
Post by David Brown
Post by Thiago Adams
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html
<snip>
Post by David Brown
Post by Thiago Adams
Would you like to see this if on C?
I would rather see it in a manner that does not need the extra clause -
or at least, the extra clause should be optional.
if (bool b = foo(); b) ...
{
bool b = foo();
if (b) ...
}
(Note the critical extra set of brackets.)
if (bool b = foo()) ...
But that would not serve all the intended purposes:

if (int n = expensive(); 2 < x && x < 17) ...
--
Ben.
David Brown
2017-05-10 15:23:41 UTC
Permalink
Raw Message
Post by Ben Bacarisse
<snip>
Post by David Brown
Post by Thiago Adams
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html
<snip>
Post by David Brown
Post by Thiago Adams
Would you like to see this if on C?
I would rather see it in a manner that does not need the extra clause -
or at least, the extra clause should be optional.
if (bool b = foo(); b) ...
{
bool b = foo();
if (b) ...
}
(Note the critical extra set of brackets.)
if (bool b = foo()) ...
if (int n = expensive(); 2 < x && x < 17) ...
I assume you meant to write "if (int x = expensive(); 2 < x && x < 17)".

And in C++, you might well have a class object in the initialiser and
want to apply a method to it.

Yes, the second clause is sometimes needed - but often it is not, and I
think it would be nice if it were optional.
Ben Bacarisse
2017-05-10 15:40:37 UTC
Permalink
Raw Message
David Brown <***@hesbynett.no> writes:
<snip>
Post by David Brown
Yes, the second clause is sometimes needed - but often it is not, and I
think it would be nice if it were optional.
I misunderstood. I thought you didn't want it at all. Sorry.
--
Ben.
David Brown
2017-05-10 20:22:29 UTC
Permalink
Raw Message
Post by Ben Bacarisse
<snip>
Post by David Brown
Yes, the second clause is sometimes needed - but often it is not, and I
think it would be nice if it were optional.
I misunderstood. I thought you didn't want it at all. Sorry.
I would rather see it in a manner that does not need the extra clause -
or at least, the extra clause should be optional.
So I expressed myself rather badly there.

You have to listen to what I /mean/, not what I /say/ :-)
Thiago Adams
2017-05-10 13:44:02 UTC
Permalink
Raw Message
Post by Thiago Adams
C++ 17, has now a new if.
[...]
Post by Thiago Adams
Would you like to see this if on C?
(switch also receive this upgrade)

Adding more ...

C++ 17 also have news on preprocessor.

__has_include

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0061r0.html

And a new assert with no message

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3928.pdf

static_assert ( constant-expression ) ;

One more:

Deprecate ++ on bool. (this could be applied to _Bool)
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0002r1.html


Would you like to see these features on C?
David Brown
2017-05-10 14:38:41 UTC
Permalink
Raw Message
Post by Thiago Adams
Post by Thiago Adams
C++ 17, has now a new if.
[...]
Post by Thiago Adams
Would you like to see this if on C?
(switch also receive this upgrade)
Adding more ...
C++ 17 also have news on preprocessor.
__has_include
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0061r0.html
Personally, I have no use for that. If I want to include a file, I do -
I don't have use-cases myself for files that might or might not be
available. If other people see it as a useful feature, that's fine.
Post by Thiago Adams
And a new assert with no message
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3928.pdf
static_assert ( constant-expression ) ;
That makes a lot of sense to me, and is a feature I would use.
Sometimes I want my static assertions to have explicit messages, but
more often it would be neater and shorter to have a simple failure message.
Post by Thiago Adams
Deprecate ++ on bool. (this could be applied to _Bool)
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0002r1.html
I can't see any use for incrementing bool's (nor for many other
operators). So I see no problem in deprecating bool ++. However, other
people might have written code that uses it, and changes to C need to be
very conservative about breaking something that worked before.
Post by Thiago Adams
Would you like to see these features on C?
GOTHIER Nathan
2017-05-10 14:22:15 UTC
Permalink
Raw Message
On Wed, 10 May 2017 06:29:33 -0700 (PDT)
Post by Thiago Adams
Would you like to see this if on C?
No because C shouldn't deserve to be as bloated as C++. Once more Bjarne
STROUSTRUP has soiled the C name with his counterfeit language that should have
been named the STROUSTRUP language instead of C++ to trick new programmers with
bad concepts.
Thiago Adams
2017-05-10 14:29:57 UTC
Permalink
Raw Message
Post by GOTHIER Nathan
On Wed, 10 May 2017 06:29:33 -0700 (PDT)
Post by Thiago Adams
Would you like to see this if on C?
No because C shouldn't deserve to be as bloated as C++. Once more Bjarne
STROUSTRUP has soiled the C name with his counterfeit language that should have
been named the STROUSTRUP language instead of C++ to trick new programmers with
bad concepts.
Probably you are against the previous insertions at C as well, like _Staticassert and _Bool.
Am I right?
The language become bigger after that.
GOTHIER Nathan
2017-05-10 15:40:16 UTC
Permalink
Raw Message
On Wed, 10 May 2017 07:29:57 -0700 (PDT)
Post by Thiago Adams
Probably you are against the previous insertions at C as well, like _Staticassert and _Bool.
Am I right?
The language become bigger after that.
It is not a matter of size but conciseness.

That's why some programmers prefer C to the STROUSTRUP language.

Stacking flawed concepts doesn't make a good language. Likewise the nouveauté
is not synonym of efficiency and at some point the programmer have to use his
neurons to write good softwares.
s***@casperkitty.com
2017-05-10 15:48:54 UTC
Permalink
Raw Message
Post by Thiago Adams
C++ 17, has now a new if.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html
if ( for-init-statement condition ) statement
is equivalent to
{
for-init-statement
if ( condition ) statement
}
Would you like to see this if on C?
What would be the effect of:

typedef struct { int x; } foo;

extern void actOnFoo(foo *p);
extern void someFunction(void);

void test()
{
foo *p;
p = (foo*){1};
if (int temp = someFunction())
p = (foo*){temp};
actOnFoo(p);
}

If "temp" had been declared prior to the "if" within the outer scoping block,
the lifetime of the compound literal within the "if" statement would extend
to the end of the function. Adding a scoping block to contain "temp" would
limit the lifetime of the compound literal to that of "temp".

Personally, I think the lifetime rules for compound literals are silly, and
the lifetime should extend either until code exits the *function* wherein a
literal is defined or execution re-reaches the point where it is created
(whereupon the lifetime of the original would end, and that of a new compound
literal would begin), but with the rules as they are anything that would
implicitly add a new scoping block should be considered dangerous.
David Brown
2017-05-10 20:24:51 UTC
Permalink
Raw Message
Post by s***@casperkitty.com
Post by Thiago Adams
C++ 17, has now a new if.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html
if ( for-init-statement condition ) statement
is equivalent to
{
for-init-statement
if ( condition ) statement
}
Would you like to see this if on C?
typedef struct { int x; } foo;
extern void actOnFoo(foo *p);
extern void someFunction(void);
void test()
{
foo *p;
p = (foo*){1};
if (int temp = someFunction())
p = (foo*){temp};
actOnFoo(p);
}
What effect are you looking for? A compiler error? A failed code review?
Thiago Adams
2017-05-10 16:40:20 UTC
Permalink
Raw Message
Post by Thiago Adams
C++ 17, has now a new if.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html
if ( for-init-statement condition ) statement
is equivalent to
{
for-init-statement
if ( condition ) statement
}
and
if ( for-init-statement condition ) statement else statement
is equivalent to
{
for-init-statement
if ( condition ) statement else statement
}
Would you like to see this if on C?
These samples look good for me:


if (Box* pBox = Shape_As_Box(pShape), pBox)
{
//pBox ...
}


if (FILE* file = fopen(""), file)
{
//file...
fclose(file);
}

if (Type* p = malloc(sizeof * p), p)
{
}
Ben Bacarisse
2017-05-10 18:15:49 UTC
Permalink
Raw Message
Post by Thiago Adams
Post by Thiago Adams
C++ 17, has now a new if.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html
if ( for-init-statement condition ) statement
<snip>
Post by Thiago Adams
Post by Thiago Adams
Would you like to see this if on C?
if (Box* pBox = Shape_As_Box(pShape), pBox)
{
//pBox ...
}
Are deliberately making it different to the C++ form or was the comma a
typo?

<snip>
--
Ben.
Thiago Adams
2017-05-10 18:22:53 UTC
Permalink
Raw Message
Post by Ben Bacarisse
Post by Thiago Adams
Post by Thiago Adams
C++ 17, has now a new if.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html
if ( for-init-statement condition ) statement
<snip>
Post by Thiago Adams
Post by Thiago Adams
Would you like to see this if on C?
if (Box* pBox = Shape_As_Box(pShape), pBox)
{
//pBox ...
}
Are deliberately making it different to the C++ form or was the comma a
typo?
The comma was my mistake.
bartc
2017-05-10 17:06:30 UTC
Permalink
Raw Message
Post by Thiago Adams
C++ 17, has now a new if.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html
if ( for-init-statement condition ) statement
is equivalent to
{
for-init-statement
if ( condition ) statement
}
and
if ( for-init-statement condition ) statement else statement
is equivalent to
{
for-init-statement
if ( condition ) statement else statement
}
Would you like to see this if on C?
Not me. I prefer declarations all tucked away in a corner so they don't
clutter up the code.

People complain about Hungarian notation but this doesn't seem much
different.

Besides, this can be done now with {} either surrounding the if
statement (assuming the scope extends to the end of the second
statement), or inside the condition using the gcc extension.
--
bartc
Ben Bacarisse
2017-05-10 18:19:59 UTC
Permalink
Raw Message
Post by bartc
Post by Thiago Adams
C++ 17, has now a new if.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html
if ( for-init-statement condition ) statement
is equivalent to
{
for-init-statement
if ( condition ) statement
}
and
if ( for-init-statement condition ) statement else statement
is equivalent to
{
for-init-statement
if ( condition ) statement else statement
}
Would you like to see this if on C?
Not me. I prefer declarations all tucked away in a corner so they
don't clutter up the code.
I prefer to have my scopes all tucked away so the names don't clutter up
the rest of the code. That way I have smaller regions of code to verify.

<snip>
--
Ben.
bartc
2017-05-10 19:19:28 UTC
Permalink
Raw Message
Post by Ben Bacarisse
Post by bartc
Not me. I prefer declarations all tucked away in a corner so they
don't clutter up the code.
I prefer to have my scopes all tucked away so the names don't clutter up
the rest of the code. That way I have smaller regions of code to verify.
Then it means having to analyse every line back to the start of the
function to find the declaration of some variable. Just in case it might
have been declared somewhere in between.

Here's one example: https://pastebin.com/iVtbFN2U where most names are
declared at the start, but some are defined in situ which means you
can't just immediately go to the top and see a cast list of all the names.

Here's another example: https://pastebin.com/v4h6Aabx where nothing is
declared at the top other than parameter names.

And it uses one of those styles where variable names all tend to look
alike. But some of them are actually identical, which you don't know
until you've scanned the entire function (there are two zWhere's for
example, fortunately of the same type).
--
bartc
Thiago Adams
2017-05-10 20:21:02 UTC
Permalink
Raw Message
Post by bartc
Post by Ben Bacarisse
Post by bartc
Not me. I prefer declarations all tucked away in a corner so they
don't clutter up the code.
I prefer to have my scopes all tucked away so the names don't clutter up
the rest of the code. That way I have smaller regions of code to verify.
Then it means having to analyse every line back to the start of the
function to find the declaration of some variable. Just in case it might
have been declared somewhere in between.
Do you mean
int i;
if ()
{
int i; //like this ??
}
Loading...