Article View: comp.std.c
Article #4882Re: integral types in switch expressions
From: Konrad Schwarz
Date: Tue, 12 Mar 1996 12:52
Date: Tue, 12 Mar 1996 12:52
36 lines
2269 bytes
2269 bytes
In article <Dnt4vo.Gu0@tigadmin.ml.com>, |> pardoej@lonnds.ml.com (Julian Pardoe LADS LDN X1428) writes: |> In article <DnKnzv.8t6%spenford@zoo.toronto.edu>, |> Henry Spencer <henry@zoo.toronto.edu> |> explains that a compiler might not be able to use an |> implementation clever than an |> if/elsif chain when compiling a switch on a pointer |> value and then goes on to say: |> |> -->. . . When the values are not known at compile time, |> -->it's hard to implement the switch as anything other than a series of |> -->ifs... and there is no point in writing that as a switch. |> |> Yes there is! It might be clearer, more readable code. The fact that the |> compiler has to implement it in a certain way is not really relevant. |> |> This isn't a big deal in C because one can't really define new types with |> enum-like behaviour but I really miss a generalized switch in C++. If I |> can switch on an enum why can't I switch on a value of some class that wraps |> up a simple value? The fact that the compiler has to use an if/elsif chain rather |> than a jump table is its problem, not mine! I'm glad someone else sees a need for a generalized switch statement. I'd be satisfied with the type of constant expression that can be used in intializers, i.e., a null pointer constant, an address constant, or an address constant plus or minus an integral constant expression, as defined in ANSI 6.4 Constant expressions, so that no changes to linkers are required. Constants of type ``address constant plus or minus an integral constant expression'' are of course prime targets for jump tables, especially if the set of constant expressions constitute a range. The one optimization you can't do is binary search for the correct label, since you can't order the labels at compile time. Konrad Schwarz
Technical Details
Message-ID:
<4i3s1r$s4o@news.tuwien.ac.at>Path:
arc1-ca.pugleaf.net!mips.complang.tuwien.ac.at!schwarzReferences:
<DnKnzv.8t6%spenford@zoo.toronto.edu> <Dnt4vo.Gu0@tigadmin.ml.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!mips.complang.tuwien.ac.at!schwarz From: schwarz@mips.complang.tuwien.ac.at (Konrad Schwarz) Newsgroups: comp.std.c Subject: Re: integral types in switch expressions Date: 12 Mar 1996 12:52:11 GMT Organization: TU Wien Message-ID: <4i3s1r$s4o@news.tuwien.ac.at> References: <DnKnzv.8t6%spenford@zoo.toronto.edu> <Dnt4vo.Gu0@tigadmin.ml.com> NNTP-Posting-Host: mips.complang.tuwien.ac.at