File: existingtypes.yo

package info (click to toggle)
c%2B%2B-annotations 12.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,044 kB
  • sloc: cpp: 24,337; makefile: 1,517; ansic: 165; sh: 121; perl: 90
file content (45 lines) | stat: -rw-r--r-- 2,059 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Once a type name has been defined it also prevails over identifiers
representing variables, if the compiler is given a choice. This, too, can
result in interesting constructions.

Assume a function tt(process) expecting an tt(int) exists in a library. We
want to use this function to process some tt(int) data values. So in tt(main)
tt(process) is declared and called:
        verb(    int process(int Data);
    process(argc);)

No problems here. But unfortunately we once decided to `beautify' our
code, by throwing in some superfluous parentheses, like so:
        verb(    int process(int (Data));
    process(argc);)

Now we're in trouble. The compiler now generates an error, caused by its
rule to let declarations prevail over definitions. tt(Data) now becomes the
name of the tt(class Data), and analogous to tt(int (x)) the parameter tt(int
(Data)) is parsed as tt(int (*)(Data)): a pointer to a function, expecting a
tt(Data) object, returning an tt(int). 

Here is another example. When, instead of declaring 
        verb(    int process(int Data[10]);)

we declare, e.g., to emphasize the fact that an array is passed to
tt(process):
        verb(    int process(int (Data[10]));)

the tt(process) function does not expect a pointer to tt(int) values, but
a pointer to a function expecting a pointer to tt(Data) elements, returning an
tt(int). 

To summarize the findings in the `Ambiguity Resolution' section:
    itemization(
    it() The compiler will try to remove superfluous parentheses;
    it() But if the parenthesized construction represents a type, it will try
        to use the type;
    it() More in general: when possible the compiler will interpret a
        syntactic construction as a declaration, rather than as a definition
        (of an object or variable).
    it() Most problems that result from the compiler interpreting
        constructions as declarations are caused by us using parentheses. As a
        i(rule of thumb): use curly braces, rather than parentheses when
        constructing objects (or values).
    )