File: polysemous.d

package info (click to toggle)
gcc-arm-none-eabi 15%3A12.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 959,712 kB
  • sloc: cpp: 3,275,382; ansic: 2,061,766; ada: 840,956; f90: 208,513; makefile: 76,132; asm: 73,433; xml: 50,448; exp: 34,146; sh: 32,436; objc: 15,637; fortran: 14,012; python: 11,991; pascal: 6,787; awk: 4,779; perl: 3,054; yacc: 338; ml: 285; lex: 201; haskell: 122
file content (71 lines) | stat: -rw-r--r-- 2,207 bytes parent folder | download | duplicates (9)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// PERMUTE_ARGS:

// Prefer immutable(char)[] to all others
int foo(           char[] a) { return 11; }
int foo(    const  char[] a) { return 12; }
int foo(immutable  char[] a) { return 13; }
int foo(          wchar[] a) { return 21; }
int foo(    const wchar[] a) { return 22; }
int foo(immutable wchar[] a) { return 23; }
int foo(          dchar[] a) { return 31; }
int foo(    const dchar[] a) { return 32; }
int foo(immutable dchar[] a) { return 33; }

// Prefer const conversion over polysemous conversion
int bar(           char[] a) { return 11; }
int bar(    const  char[] a) { return 12; }
//  bar(immutable  char[] a);
int bar(          wchar[] a) { return 21; }
int bar(    const wchar[] a) { return 22; }
//  bar(immutable wchar[] a);
int bar(          dchar[] a) { return 31; }
int bar(    const dchar[] a) { return 32; }
//  bar(immutable dchar[] a);

// No conversion to mutable
int baz(           char[] a) { return 11; }
//  baz(    const  char[] a);
//  baz(immutable  char[] a);
int baz(          wchar[] a) { return 21; }
//  baz(    const wchar[] a);
//  baz(immutable wchar[] a);
int baz(          dchar[] a) { return 31; }
//  baz(    const dchar[] a);
//  baz(immutable dchar[] a);

int main()
{
    auto strn = "a";
    auto strc = "a"c;
    auto strw = "a"w;
    auto strd = "a"d;

    assert(foo("a" ) == 13);
    assert(foo(strn) == 13);
    assert(foo("a"c) == 13);
    assert(foo(strc) == 13);
    assert(foo("a"w) == 23);
    assert(foo(strw) == 23);
    assert(foo("a"d) == 33);
    assert(foo(strd) == 33);

    assert(bar("a" ) == 12);
    assert(bar(strn) == 12);
    assert(bar("a"c) == 12);
    assert(bar(strc) == 12);
    assert(bar("a"w) == 22);
    assert(bar(strw) == 22);
    assert(bar("a"d) == 32);
    assert(bar(strd) == 32);

    static assert(!__traits(compiles, baz("a" ) ));
    static assert(!__traits(compiles, baz(strn) ));
    static assert(!__traits(compiles, baz("a"c) ));
    static assert(!__traits(compiles, baz(strc) ));
    static assert(!__traits(compiles, baz("a"w) ));
    static assert(!__traits(compiles, baz(strw) ));
    static assert(!__traits(compiles, baz("a"d) ));
    static assert(!__traits(compiles, baz(strd) ));

    return 0;
}