C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integr

Discussion in 'C Programming' started by Adem, Oct 23, 2008.

  1. Adem

    Adem Guest

    C/C++ language proposal:
    Change the 'case expression' from "integral constant-expression" to "integral expression"

    The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
    says under 6.4.2(2) [see also 5.19]:

    case constant-expression :

    I propose that the case expression of the switch statement
    be changed from "integral constant-expression" to "integral expression".
    This opens up many new possibilities since then also function calls
    would be permitted in the case expression.
    The old case case would continue to function since
    it is a subset of the new case case.

    Example usage:

    //...
    int f()
    {
    //...
    return BLA1;
    }

    int g()
    {
    //...
    return BLA2;
    }

    int h()
    {
    //...
    return BLA3;
    }

    int y, x = f();
    switch (x)
    {
    case 123 : y = g(); break;
    case g() : y = 456; break; // using new case feature, ie. func-call
    case h() : y = 789; break; // ditto
    default : y = -1; break;
    }
    Adem, Oct 23, 2008
    #1
    1. Advertising

  2. Adem

    Lew Pitcher Guest

    On October 23, 2008 14:17, in comp.lang.c, Adem ()
    wrote:

    > C/C++ language proposal:
    > Change the 'case expression' from "integral constant-expression" to
    > "integral expression"
    >
    > The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
    > says under 6.4.2(2) [see also 5.19]:

    [snip]

    Sorry, but your proposal is off-topic for the comp.lang.c newsgroup. I
    suspect that it is also off-topic for comp.lang.c++.

    As C and C++ are two separate languages, each defined by their own
    standards, I suggest that you break your "C/C++" proposal into a "C
    proposal" and a separate "C++ proposal", and submit each to their
    respective standards bodies.

    For C, you /might/ post your proposal to the comp.std.c newsgroup, as that
    is the group that discusses the ramifications of and enhancements to the C
    language standard.

    --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    ---------- Slackware - Because I know what I'm doing. ------
    Lew Pitcher, Oct 23, 2008
    #2
    1. Advertising

  3. Eric Sosman <> writes:
    > Adem wrote:
    >> C/C++ language proposal: Change the 'case expression' from
    >> "integral constant-expression" to "integral expression"
    >> The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
    >> says under 6.4.2(2) [see also 5.19]:
    >> case constant-expression : I propose that the case expression
    >> of the switch statement
    >> be changed from "integral constant-expression" to "integral expression".

    >
    > switch (rand()) {
    > case rand(): puts ("What's"); break;
    > case rand(): puts ("wrong"); break;
    > case rand(): puts ("with"); break;
    > case rand(): puts ("this"); break;
    > case rand(): puts ("picture?"); break;
    > }


    Well, it *could* be defined to be equivalent to the following:

    {
    const int __tmp = rand();
    if (__tmp == rand() puts("What's");
    else if (__tmp == rand() puts("wrong");
    else if (__tmp == rand() puts("with");
    else if (__tmp == rand() puts("this");
    else if (__tmp == rand() puts("picture?");
    }

    That's intended to be equivalent code just for this example, not a
    definition; other transformations would be required when one case
    falls through to the next. And I refuse to think about how this would
    affect Duff's Device.

    The point of C's switch statement is that it can be implemented
    efficiently as a jump table, in contrast to an if/else chain where the
    conditions have to be evaluated sequentially.

    If you want to do something like a switch statement, but where the
    case values aren't constant, you can always just write an if/else
    chain. If you want to support this kind of thing in the language, so
    that you only have to specify the LHS of the comparison once, I think
    I'd prefer to introduce a new construct rather than adding this
    functionality to the existing switch/case construct. With this
    proposal, changing a single expression from a constant to a
    non-constant could have substantial effects on the generated code, and
    depending on how it's defined, subtle effects on the code's behavior.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 23, 2008
    #3
  4. Adem

    Guest

    Re: C/C++ language proposal: Change the 'case expression' from"integral constant-expression" to "integral expression"

    Gordon Burditt wrote:
    > >C/C++ language proposal:
    > > Change the 'case expression' from "integral constant-expression" to
    > >"integral expression"
    > >
    > >The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
    > >says under 6.4.2(2) [see also 5.19]:
    > >
    > > case constant-expression :
    > >
    > >I propose that the case expression of the switch statement
    > >be changed from "integral constant-expression" to "integral expression".
    > >This opens up many new possibilities since then also function calls
    > >would be permitted in the case expression.

    ....
    > Actually using this feature would lose a lot of optimization
    > (compiling it using branch tables or binary search trees).
    > Compilers go to a lot of effort NOT to compile a switch as
    > a bunch of nested if ... else if ... else if ... else ... .


    This proposal only relaxes the syntax rules, while failing to update
    the semantic specification so that it makes sense in those cases that
    used to be syntax errors. However, so long as the changes to the
    semantics of switch statements do not change the behavior of code that
    is not currently a syntax error, such switch statements could still be
    optimized in the same way that they currently are.
    , Oct 23, 2008
    #4
  5. Adem

    0m Guest

    Re: C/C++ language proposal: Change the 'case expression' from"integral constant-expression" to "integral expression"

    On Oct 24, 3:12 am, Keith Thompson <> wrote:
    > Eric Sosman <> writes:
    > > Adem wrote:
    > >> C/C++ language proposal: Change the 'case expression' from
    > >> "integral constant-expression" to "integral expression"
    > >> The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
    > >> says under 6.4.2(2) [see also 5.19]:
    > >> case constant-expression : I propose that the case expression
    > >> of the switch statement
    > >> be changed from "integral constant-expression" to "integral expression".

    >
    > > switch (rand()) {
    > > case rand(): puts ("What's"); break;
    > > case rand(): puts ("wrong"); break;
    > > case rand(): puts ("with"); break;
    > > case rand(): puts ("this"); break;
    > > case rand(): puts ("picture?"); break;
    > > }


    Wonderful!

    >
    > Well, it *could* be defined to be equivalent to the following:
    >
    > {
    > const int __tmp = rand();
    > if (__tmp == rand() puts("What's");
    > else if (__tmp == rand() puts("wrong");
    > else if (__tmp == rand() puts("with");
    > else if (__tmp == rand() puts("this");
    > else if (__tmp == rand() puts("picture?");
    >
    > }
    >


    If rand() generates equal random numbers frequently, is it meaningful?
    0m, Oct 24, 2008
    #5
  6. Adem

    James Kuyper Guest

    Re: C/C++ language proposal: Change the 'case expression' from "integralconstant-expression" to "integral expression"

    0m wrote:
    > On Oct 24, 3:12 am, Keith Thompson <> wrote:

    ....
    >> Well, it *could* be defined to be equivalent to the following:
    >>
    >> {
    >> const int __tmp = rand();
    >> if (__tmp == rand() puts("What's");
    >> else if (__tmp == rand() puts("wrong");
    >> else if (__tmp == rand() puts("with");
    >> else if (__tmp == rand() puts("this");
    >> else if (__tmp == rand() puts("picture?");
    >>
    >> }
    >>

    >
    > If rand() generates equal random numbers frequently, is it meaningful?


    That code neither makes nor implies any assumptions about how frequently
    rand() generates random numbers.
    James Kuyper, Oct 24, 2008
    #6
  7. Adem

    Bartc Guest

    "Eric Sosman" <> wrote in message
    news:1224788466.760836@news1nwk...
    > Adem wrote:
    >> C/C++ language proposal: Change the 'case expression' from "integral
    >> constant-expression" to "integral expression"
    >>
    >> The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
    >> says under 6.4.2(2) [see also 5.19]:
    >>
    >> case constant-expression : I propose that the case expression of
    >> the switch statement
    >> be changed from "integral constant-expression" to "integral expression".

    >
    > switch (rand()) {
    > case rand(): puts ("What's"); break;
    > case rand(): puts ("wrong"); break;
    > case rand(): puts ("with"); break;
    > case rand(): puts ("this"); break;
    > case rand(): puts ("picture?"); break;
    > }


    What's wrong with it that you're just injecting garbage. You can use the
    same rand() trick with any statement:

    for (i=rand(); i<=rand(); i=rand())...

    But there's nothing really wrong with the OP's proposal, except it should be
    a new statement type to keep the switch semantics clean. Then, it could also
    use non-integer types.

    C likes it's rather austere syntax however so I doubt this change will ever
    be made.

    --
    Bartc
    Bartc, Oct 24, 2008
    #7
  8. Adem

    anon Guest

    Re: C/C++ language proposal: Change the 'case expression' from "integralconstant-expression" to "integral expression"

    Bartc wrote:
    >
    > "Eric Sosman" <> wrote in message
    > news:1224788466.760836@news1nwk...
    >> Adem wrote:
    >>> C/C++ language proposal: Change the 'case expression' from "integral
    >>> constant-expression" to "integral expression"
    >>>
    >>> The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
    >>> says under 6.4.2(2) [see also 5.19]:
    >>>
    >>> case constant-expression : I propose that the case expression
    >>> of the switch statement
    >>> be changed from "integral constant-expression" to "integral expression".

    >>
    >> switch (rand()) {
    >> case rand(): puts ("What's"); break;
    >> case rand(): puts ("wrong"); break;
    >> case rand(): puts ("with"); break;
    >> case rand(): puts ("this"); break;
    >> case rand(): puts ("picture?"); break;
    >> }

    >
    > What's wrong with it that you're just injecting garbage. You can use the
    > same rand() trick with any statement:
    >
    > for (i=rand(); i<=rand(); i=rand())...
    >
    > But there's nothing really wrong with the OP's proposal, except it
    > should be a new statement type to keep the switch semantics clean. Then,
    > it could also use non-integer types.


    There is something wrong. Try to compile this example, and I hope you
    figure whats wrong with his proposal :

    int main()
    {
    int a = 3;
    switch( a )
    {
    case 3:
    a = 1;
    break;
    case 3:
    a = 2;
    break;
    case 3:
    a = 3;
    break;
    default:
    a = 4;
    }
    }
    anon, Oct 24, 2008
    #8
  9. Adem

    Bartc Guest

    "anon" <> wrote in message
    news:gdsjtj$1e4$...
    > Bartc wrote:
    >>
    >> "Eric Sosman" <> wrote in message
    >> news:1224788466.760836@news1nwk...
    >>> Adem wrote:
    >>>> C/C++ language proposal: Change the 'case expression' from "integral
    >>>> constant-expression" to "integral expression"


    >> But there's nothing really wrong with the OP's proposal, except it should
    >> be a new statement type to keep the switch semantics clean. Then, it
    >> could also use non-integer types.

    >
    > There is something wrong. Try to compile this example, and I hope you
    > figure whats wrong with his proposal :
    >
    > int main()
    > {
    > int a = 3;
    > switch( a )
    > {
    > case 3:
    > a = 1;
    > break;
    > case 3:
    > a = 2;
    > break;
    > case 3:
    > a = 3;
    > break;
    > default:
    > a = 4;
    > }
    > }


    The OP was talking about /expressions/ for case values. But because normal
    switch likes it's values to be unique, I suggested a different statement.
    This way duplicate case values can still be picked up instead of being
    quietly converted to the new semantics:

    a=x=y=z=3;

    newswitch (a)
    {
    case x: ...
    case y: ...
    case z: ...
    ...
    This would just select the first matching expression. No different from a
    series of if/else statements as already noted, but more eloquent.

    --
    Bartc
    Bartc, Oct 24, 2008
    #9
  10. Adem

    Bo Persson Guest

    Bartc wrote:
    > "anon" <> wrote in message
    > news:gdsjtj$1e4$...
    >> Bartc wrote:
    >>>
    >>> "Eric Sosman" <> wrote in message
    >>> news:1224788466.760836@news1nwk...
    >>>> Adem wrote:
    >>>>> C/C++ language proposal: Change the 'case expression' from
    >>>>> "integral constant-expression" to "integral expression"

    >
    >>> But there's nothing really wrong with the OP's proposal, except
    >>> it should be a new statement type to keep the switch semantics
    >>> clean. Then, it could also use non-integer types.

    >>
    >> There is something wrong. Try to compile this example, and I hope
    >> you figure whats wrong with his proposal :
    >>
    >> int main()
    >> {
    >> int a = 3;
    >> switch( a )
    >> {
    >> case 3:
    >> a = 1;
    >> break;
    >> case 3:
    >> a = 2;
    >> break;
    >> case 3:
    >> a = 3;
    >> break;
    >> default:
    >> a = 4;
    >> }
    >> }

    >
    > The OP was talking about /expressions/ for case values. But because
    > normal switch likes it's values to be unique, I suggested a
    > different statement. This way duplicate case values can still be
    > picked up instead of being quietly converted to the new semantics:
    >
    > a=x=y=z=3;
    >
    > newswitch (a)
    > {
    > case x: ...
    > case y: ...
    > case z: ...
    > ...
    > This would just select the first matching expression. No different
    > from a series of if/else statements as already noted, but more
    > eloquent.


    Yes, it would work, just as it does for COBOL's EVALUATE statement - a
    multiway if-statement

    http://www.fluffycat.com/COBOL/Evaluate/


    Wanna be more like COBOL? :)


    Bo Persson
    Bo Persson, Oct 24, 2008
    #10
  11. Adem

    0m Guest

    Re: C/C++ language proposal: Change the 'case expression' from"integral constant-expression" to "integral expression"

    On Oct 24, 7:59 pm, "Bartc" <> wrote:
    > "Eric Sosman" <> wrote in message
    > > Adem wrote:
    > >> The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
    > >> says under 6.4.2(2) [see also 5.19]:
    > >> case constant-expression : I propose that the case expression of
    > >> the switch statement
    > >> be changed from "integral constant-expression" to "integral expression".

    > > switch (rand()) {
    > > case rand(): puts ("What's"); break;
    > > case rand(): puts ("wrong"); break;
    > > case rand(): puts ("with"); break;
    > > case rand(): puts ("this"); break;
    > > case rand(): puts ("picture?"); break;
    > > }

    > What's wrong with it that you're just injecting garbage. You can use the
    > same rand() trick with any statement:
    >
    > for (i=rand(); i<=rand(); i=rand())...


    The statements following each case lable in a switch are intended to
    be diffrent, while the body of for statement is the same for each
    loop. Eric's trick illustrates that how bad an idean could that
    proposal be.
    0m, Oct 24, 2008
    #11
  12. Re: C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression"

    "0m" <> writes:
    > On Oct 24, 7:59 pm, "Bartc" <> wrote:
    >> "Eric Sosman" <> wrote in message
    >> > Adem wrote:
    >> >> The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
    >> >> says under 6.4.2(2) [see also 5.19]:
    >> >> case constant-expression : I propose that the case expression of
    >> >> the switch statement
    >> >> be changed from "integral constant-expression" to "integral expression".
    >> > switch (rand()) {
    >> > case rand(): puts ("What's"); break;
    >> > case rand(): puts ("wrong"); break;
    >> > case rand(): puts ("with"); break;
    >> > case rand(): puts ("this"); break;
    >> > case rand(): puts ("picture?"); break;
    >> > }

    >> What's wrong with it that you're just injecting garbage. You can use the
    >> same rand() trick with any statement:
    >>
    >> for (i=rand(); i<=rand(); i=rand())...

    >
    > The statements following each case lable in a switch are intended to
    > be diffrent, while the body of for statement is the same for each
    > loop. Eric's trick illustrates that how bad an idean could that
    > proposal be.


    The expressions (not statements) are intended (required, in fact) to
    be integer constant expressions. The proposal is to change that
    requirement. It's moderately obvious that the requirement that no two
    of the case expressions would also have to be relaxed, since it's not
    possible in general to determine at compilation time whether two
    non-constant expressions have the same value.

    Defining consistent semantics for a modified switch statement is not
    all that difficult. Presumably existing switch statements would have
    to behave as they do now. Beyond that, there are several possible
    ways to define the semantics and constraints for switch statements
    that are currently invalid.

    Eric's example demonstrates the need to define the semantics of this
    proposed new feature. I don't think it demonstrates, by itself, that
    it's a bad idea. If non-constant switch expressions were allowed, it
    would be possible to write perfectly sensible code using the new
    feature. It would also, as Eric demonstrated, be possible to write
    very silly code using the new feature -- or using any of the
    language's existing features.

    I do agree that the proposed feature is a bad idea, but not because of
    Eric's example. I think it's a bad idea because it would have to
    define new semantics for an existing construct. Currently, if you
    accidentally use a non-constant case expression, the compiler will
    diagnose it as an error (and probably reject your program). Under the
    proposal, the code would be valid, and would likely behave in a subtly
    wrong manner and/or with poor performance. Evaluating the switch
    expression and branching to the right case label is typically an O(1)
    operation, if the compiler is able to generate a jump table. Add a
    single non-constant expression, and it becomes O(N), where N is the
    number of cases.

    I wouldn't object as strongly to a new feature, with a distinct
    syntax, that implements the proposed functionality, but I'd still
    oppose it -- not because it's an inherently bad idea, but because the
    benefit is IMHO outweighed by the cost of adding a new feature to the
    language. There's nothing you could do with the proposed new feature
    that you can't already do fairly straightforwardly using existing
    features, namely an if/else chain and, if the switch expression is
    complicated or has side effects, a temporary variable.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 24, 2008
    #12
  13. Adem

    CBFalconer Guest

    Re: C/C++ language proposal: Change the 'case expression' from"integralconstant-expression" to "integral expression"

    anon wrote:
    >

    .... snip ...
    >
    > There is something wrong. Try to compile this example, and I hope
    > you figure whats wrong with his proposal :
    >
    > int main()
    > {
    > int a = 3;
    > switch( a )
    > {
    > case 3:
    > a = 1;
    > break;
    > case 3:
    > a = 2;
    > break;
    > case 3:
    > a = 3;
    > break;
    > default:
    > a = 4;
    > }
    > }


    Your code works better if it is made legal:

    int main(void) {
    int a = 3;

    switch( a ) {
    case 3: a = 1;
    break;
    case 2: a = 2;
    break;
    case 1: a = 3;
    break;
    default: a = 4;
    }
    return 0;
    }

    Also, cross posting to c.l.c++ is not cool.


    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Oct 24, 2008
    #13
  14. Adem

    Bartc Guest

    "Eric Sosman" <> wrote in message
    news:...
    > Bartc wrote:
    >>
    >> "Eric Sosman" <> wrote in message
    >> news:1224788466.760836@news1nwk...
    >>> Adem wrote:
    >>>> C/C++ language proposal: Change the 'case expression' from "integral
    >>>> constant-expression" to "integral expression"


    >>> switch (rand()) {
    >>> case rand(): puts ("What's"); break;
    >>> case rand(): puts ("wrong"); break;
    >>> case rand(): puts ("with"); break;
    >>> case rand(): puts ("this"); break;
    >>> case rand(): puts ("picture?"); break;


    >> What's wrong with it that you're just injecting garbage. You can use the
    >> same rand() trick with any statement:
    >>
    >> for (i=rand(); i<=rand(); i=rand())...

    >
    > This has well-defined semantics, albeit not very useful.
    > The purpose of my example wasn't to claim that it is impossible
    > to invent semantics for the O.P.'s `switch', but to show that
    > the proposal as it stands is not even close to adequate.
    >
    > Let's try another one:
    >
    > switch (x) {
    > case f(x): puts("f"); break;
    > case g(x): puts("g"); break;
    > }
    > ...
    > int f(int x) { puts("in f"); return x & 1; }
    > int g(int x) { puts("in g"); return x & 2; }
    >
    > What output do you desire when `x == 16'? Two lines?
    > Three? Four? In what order?


    When x is 16, both f() and g() return 0, which does not equal x so matches
    neither case.

    You're thinking in terms of the current switch semantics which likes unique
    case values and can take them in any order. That's why I suggested a
    different statement (or just a different keyword).

    (I tried this code in a language of mine that accepts expressions for case
    expressions. The output was "in f" followed by "in g".

    In this language there are two distinct statements: switch-when for integer
    values over a small range,expected to be implemented as a jump table (ie. C
    switch); and case-when for anything else: large integers or other types.
    Although I don't think I've ever used a non-const expression for a
    switch/case value. Until now)

    --
    Bartc
    Bartc, Oct 25, 2008
    #14
  15. Re: C/C++ language proposal: Change the 'case expression' from "integralconstant-expression" to "integral expression"

    CBFalconer <> writes:
    > anon wrote:
    >>

    > ... snip ...
    >>
    >> There is something wrong. Try to compile this example, and I hope
    >> you figure whats wrong with his proposal :
    >>
    >> int main()
    >> {
    >> int a = 3;
    >> switch( a )
    >> {
    >> case 3:
    >> a = 1;
    >> break;
    >> case 3:
    >> a = 2;
    >> break;
    >> case 3:
    >> a = 3;
    >> break;
    >> default:
    >> a = 4;
    >> }
    >> }

    >
    > Your code works better if it is made legal:
    >
    > int main(void) {
    > int a = 3;
    >
    > switch( a ) {
    > case 3: a = 1;
    > break;
    > case 2: a = 2;
    > break;
    > case 1: a = 3;
    > break;
    > default: a = 4;
    > }
    > return 0;
    > }


    Which completely and utterly misses the point.

    The article that started this thread was a proposal to allow
    non-constant case expressions. anon's point was that it would then
    become impossible (or nearly so) to maintain the property that all
    case expressions in a switch statement must have distinct values.
    Your "correction" is valid C, but pointless; you might as well have
    posted "int main(void) { }".

    > Also, cross posting to c.l.c++ is not cool.


    Perhaps, but the topic of the thread is a proposed change to both C
    and C++. Arguably the comp.std.* groups would have been better, but I
    think comp.std.c++ is more or less dead these days. Since switch
    statements have the same syntax and semantics in C and C++, it's not
    entirely unreasonable to discuss a proposed change in both.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 25, 2008
    #15
  16. Re: C/C++ language proposal: Change the 'case expression' from "integralconstant-expression" to "integral expression"

    In article <>,
    Keith Thompson <> wrote:
    ....
    >Which completely and utterly misses the point.


    It's what Chuck does best. One should always play to one's strengths.
    Kenny McCormack, Oct 25, 2008
    #16
  17. Adem

    peter koch Guest

    Re: C/C++ language proposal: Change the 'case expression' from"integralconstant-expression" to "integral expression"

    On 24 Okt., 23:56, CBFalconer <> wrote:
    > anon wrote:
    >
    > ... snip ...
    >
    > > There is something wrong. Try to compile this example, and I hope
    > > you figure whats wrong with his proposal :

    >
    > > int main()
    > > {
    > >      int a = 3;
    > >      switch( a )
    > >      {
    > >          case 3:
    > >              a = 1;
    > >              break;
    > >          case 3:
    > >              a = 2;
    > >              break;
    > >          case 3:
    > >              a = 3;
    > >              break;
    > >          default:
    > >              a = 4;
    > >      }
    > > }

    >
    > Your code works better if it is made legal:
    >
    > int main(void) {
    >      int a = 3;
    >
    >      switch( a ) {
    > case 3:  a = 1;
    >          break;
    > case 2:  a = 2;
    >          break;
    > case 1:  a = 3;
    >          break;
    > default: a = 4;
    >      }
    >      return 0;
    >
    > }


    The original code would be legal: case-labels would not be required to
    be unique in that proposal, and all the "encased" statements except
    for the first label would basically be dead.

    >
    > Also, cross posting to c.l.c++ is not cool.


    This is one of the cases where it would be: The proposal is for both C
    and C++. Hopefully the proposal will be rejected equally unanimously
    both places ;-)

    /Peter
    peter koch, Oct 25, 2008
    #17
  18. Adem

    Guest

    Re: C/C++ language proposal: Change the 'case expression' from"integralconstant-expression" to "integral expression"

    On Oct 25, 12:56 am, CBFalconer <> wrote:
    > anon wrote:
    >
    > ... snip ...
    >
    > > There is something wrong. Try to compile this example, and I hope
    > > you figure whats wrong with his proposal :

    >
    > > int main()
    > > {
    > > int a = 3;
    > > switch( a )
    > > {
    > > case 3:
    > > a = 1;
    > > break;
    > > case 3:
    > > a = 2;
    > > break;
    > > case 3:
    > > a = 3;
    > > break;
    > > default:
    > > a = 4;
    > > }
    > > }

    >
    > Your code works better if it is made legal:
    >
    > int main(void) {
    > int a = 3;
    >
    > switch( a ) {
    > case 3: a = 1;
    > break;
    > case 2: a = 2;
    > break;
    > case 1: a = 3;
    > break;
    > default: a = 4;
    > }
    > return 0;
    >
    > }


    His code _is_ legal under C99.
    , Oct 25, 2008
    #18
  19. Re: C/C++ language proposal: Change the 'case expression' from"integralconstant-expression" to "integral expression"

    On Sat, 25 Oct 2008 04:07:27 -0700, vippstar wrote:
    > On Oct 25, 12:56 am, CBFalconer <> wrote:
    >> anon wrote:
    >>
    >> ... snip ...
    >>
    >> > There is something wrong. Try to compile this example, and I hope you
    >> > figure whats wrong with his proposal :

    >>
    >> > int main()
    >> > {
    >> > int a = 3;
    >> > switch( a )
    >> > {
    >> > case 3:
    >> > a = 1;
    >> > break;
    >> > case 3:
    >> > a = 2;
    >> > break;
    >> > case 3:
    >> > a = 3;
    >> > break;
    >> > default:
    >> > a = 4;
    >> > }
    >> > }

    >>
    >> Your code works better if it is made legal:

    >
    > His code _is_ legal under C99.


    No, it's not, and intentionally so. 6.8.4.2p3:
    "The expression of each case label shall be an integer constant expression
    and no two of the case constant expressions in the same switch statement
    shall have the same value after conversion. [...]"

    Do you have any compiler that accepts the code? If so, what does it set a
    to?
    Harald van Dijk, Oct 25, 2008
    #19
  20. Adem

    Jerry Coffin Guest

    In article <gdqf6p$fqj$>, says...

    [ ... ]

    > int y, x = f();
    > switch (x)
    > {
    > case 123 : y = g(); break;
    > case g() : y = 456; break; // using new case feature, ie. func-call
    > case h() : y = 789; break; // ditto
    > default : y = -1; break;
    > }


    This has a number of ramifications that could make definition difficult.
    For a few examples, how often would g() be evaluated in the switch
    statement above? If both g() and h() returned the same value, which leg
    would be taken?

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Oct 26, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith

    integral constant expression

    Keith, Jul 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    412
    Micah Cowan
    Jul 14, 2003
  2. Replies:
    14
    Views:
    1,160
    Ian Collins
    Mar 18, 2008
  3. Adem
    Replies:
    42
    Views:
    2,773
    James Kuyper
    Nov 4, 2008
  4. penny
    Replies:
    28
    Views:
    2,325
    Charlton Wilbur
    Mar 10, 2008
  5. Lucas Malor

    Yet Another Switch-Case Syntax Proposal

    Lucas Malor, Apr 2, 2014, in forum: Python
    Replies:
    0
    Views:
    39
    Lucas Malor
    Apr 2, 2014
Loading...

Share This Page