Topic: Context switch
Author: SeeWebsiteForEmail@erdani.org ("Andrei Alexandrescu (See Website For Email)")
Date: Tue, 25 Jul 2006 19:11:41 GMT Raw View
frege wrote:
> Michiel.Salters@tomtom.com wrote:
>
>>Easier:
>>switch (int S = Function ())
>>{
>> case 0: break;
>> case 1: break;
>> default:
>> ShowMessage (S)
>> break;
>>}
>>which is consistent with the 'if (int S = Function) { }' form.
>>
>>HTH,
>>Michiel Salters
>
>
> I'm a big fan of that form of 'if', so I'll probably now use it for
> switch as well.
I'm too, and I'd be much more of a fan if variable definitions would be
allowed wherever an expression is. The current rules for defining a
variable inside a while, switch, if, or for are forced and unnecessarily
limited. You can't do for example:
while ((int c = getchar()) != EOF) { ... }
Has anyone proposed allowing definitions where expressions are allowed?
Andrei
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: "frege" <gottlobfrege@gmail.com>
Date: Tue, 25 Jul 2006 00:32:36 CST Raw View
Michiel.Salters@tomtom.com wrote:
>
> Easier:
> switch (int S = Function ())
> {
> case 0: break;
> case 1: break;
> default:
> ShowMessage (S)
> break;
> }
> which is consistent with the 'if (int S = Function) { }' form.
>
> HTH,
> Michiel Salters
I'm a big fan of that form of 'if', so I'll probably now use it for
switch as well. But some other syntax could make the name of the
switch value consistent, instead of looking up to the beginning of the
switch to find out what the variable is called.
Of course you could say the same thing about the 'if' condition, the
return value of a function (ie do you call it 'res', 'result', 'ret',
???), etc.
Tony
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: fgothamNO@SPAM.com (Frederick Gotham)
Date: Fri, 21 Jul 2006 06:32:43 GMT Raw View
> Easier:
> switch (int S = Function ())
I'd probably make the const:
switch (int const S = Func())
(Depending on the context of course.)
--
Frederick Gotham
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: Raymond <raymond@nosuch.gov>
Date: Sun, 23 Jul 2006 08:48:17 CST Raw View
Michiel.Salters@tomtom.com wrote:
> Easier:
> switch (int const S = Function ()) // *Added:* const
> {
> case 0: break;
> case 1: break;
> default:
> ShowMessage (S); // *Added:* ;
> break;
> }
> which is consistent with the 'if (int S = Function) { }' form.
I thought about using an outer block, but now that I've read the
grammar, I see the condition accepts 'type-specifier-seq declarator =
assignment-expression', as you demonstrated above, which is excellent.
But why doesn't the language accept the semantically equivalent version
in this case?
switch (int const S (Function ()))
{
default:
ShowMessage (S);
break;
}
It's a scalar object.
// int Function ();
int const a = Function ();
int const b (Function ());
Both statements are semantically the same (in this context).
--
Raymond
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: raymond@nosuch.gov (Raymond)
Date: Wed, 19 Jul 2006 22:38:44 GMT Raw View
Some words (keywords) mean different things in different contexts, just
like in a human language:
// before.cpp
int const ReturnValue (Function ());
switch (ReturnValue)
{
case 0: break;
case 1: break;
default:
ShowMessage (ReturnValue);
break;
}
// after.cpp
switch (Function ())
{
case 0: break;
case 1: break;
default:
ShowMessage (switch); // Great use.
break;
}
--
Raymond
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: "frege" <gottlobfrege@gmail.com>
Date: Wed, 19 Jul 2006 23:37:25 CST Raw View
Raymond wrote:
> Some words (keywords) mean different things in different contexts, just
> like in a human language:
>
> // before.cpp
>
> int const ReturnValue (Function ());
>
> switch (ReturnValue)
> {
> case 0: break;
> case 1: break;
> default:
> ShowMessage (ReturnValue);
> break;
> }
>
> // after.cpp
>
> switch (Function ())
> {
> case 0: break;
> case 1: break;
> default:
> ShowMessage (switch); // Great use.
> break;
> }
>
Or maybe even 'switch::value'
ShowMessage(switch::value)
if 'switch' turns out to be too ambiguous.
-Tony
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: Michiel.Salters@tomtom.com
Date: Thu, 20 Jul 2006 09:40:01 CST Raw View
frege wrote:
> Raymond wrote:
> > Some words (keywords) mean different things in different contexts, just
> > like in a human language:
> >
> > // before.cpp
> >
> > int const ReturnValue (Function ());
> >
> > switch (ReturnValue)
> > {
> > case 0: break;
> > case 1: break;
> > default:
> > ShowMessage (ReturnValue);
> > break;
> > }
> >
> > // after.cpp
> >
> > switch (Function ())
> > {
> > case 0: break;
> > case 1: break;
> > default:
> > ShowMessage (switch); // Great use.
> > break;
> > }
> >
>
> Or maybe even 'switch::value'
>
> ShowMessage(switch::value)
>
> if 'switch' turns out to be too ambiguous.
Doesn't really improve the ambiguity; when you encounter the switch
keyword
in parsing you still don't know whether it starts an expression or a
statement.
In both cases, the first token after switch will tell you whether it's
an expression
or not.
Easier:
switch (int S = Function ())
{
case 0: break;
case 1: break;
default:
ShowMessage (S)
break;
}
which is consistent with the 'if (int S = Function) { }' form.
HTH,
Michiel Salters
---
[ 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.comeaucomputing.com/csc/faq.html ]