Topic: if( int k = f() ) ok but if( (int k = f()) != 0 )


Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Wed, 30 May 2001 01:58:07 GMT
Raw View
In article <Pine.SOL.3.96.1010525111235.8450C-100000@taumet>, Steve
Clamage <clamage@eng.sun.com> writes
>The condition portion of an "if", "for" or "while" can be an expression,
>or a variable declaration with an initializer using the "=" syntax.

And the latter creates numerous problems. You cannot declare objects
that need more than one value for initialisation if they lack an
accessible copy ctor. There are also problems with types whose ctors are
qualified with explicit. Of course there is also a problem in that ctors
do not have return values, so somehow there has to be a conversion from
the type being created to a bool.  I think the whole thing is a mess.
Perhaps we should consider how it might be tidied up.

Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Steve Clamage <clamage@eng.sun.com>
Date: Fri, 25 May 2001 22:50:25 GMT
Raw View
On Thu, 24 May 2001, Steve Folly wrote:

> "Sergey Shabalov" <Shabalov_S_V@confectionery.com.ua> wrote in message
> news:3b0b9cd6$1@digi8.pi.net.ua...
> > I wrote operator
> >
> >     if( int k = f() );
> >
> > and compiler accept it but when I have changed it
> >
> > if( (int k = f()) != 0 );
> >
> > I got error message.
> >
> > What does the standard say about it ?
> >
> > Serg
>
> What error message? What compiler? What platform?

It doesn't really matter.

The condition portion of an "if", "for" or "while" can be an expression,
or a variable declaration with an initializer using the "=" syntax.

The second example is neither of those, and is therefore invalid. No
compiler should accept it.


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Thu, 24 May 2001 19:07:36 GMT
Raw View
Sergey Shabalov wrote:
>
> I wrote operator
>
>     if( int k = f() );
>
> and compiler accept it but when I have changed it
>
> if( (int k = f()) != 0 );
>
> I got error message.
>
> What does the standard say about it ?

It says in section 6.4 p1 that:

_selection-statement:_
 if ( _condition_ ) statement
 ...

_condition:_
 _expression_
 _type-specifier-seq_ _declarator_ = _assignment-expression_

Now, (int k = f()) != 0 doesn't qualify as an expression; declarations
aren't allowed in expressions. It also doesn't match the second form.
'int' is a type-specifier-seq, 'k' is a declarator, and 'f()' is an
assignment-expression, so your first example was perfectly legal, but
the stuff you added in your second example doesn't match the rule.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Steve Folly" <steve.folly@uk.thalesgroup.moc>
Date: Thu, 24 May 2001 19:07:59 GMT
Raw View
"Sergey Shabalov" <Shabalov_S_V@confectionery.com.ua> wrote in message
news:3b0b9cd6$1@digi8.pi.net.ua...
> I wrote operator
>
>     if( int k = f() );
>
> and compiler accept it but when I have changed it
>
> if( (int k = f()) != 0 );
>
> I got error message.
>
> What does the standard say about it ?
>
> Serg

What error message? What compiler? What platform?

SF


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Sergey Shabalov" <Shabalov_S_V@confectionery.com.ua>
Date: Wed, 23 May 2001 15:06:30 GMT
Raw View
I wrote operator

    if( int k = f() );

and compiler accept it but when I have changed it

if( (int k = f()) != 0 );

I got error message.

What does the standard say about it ?

Serg


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]