File: variadic.cc

package info (click to toggle)
c%2B%2B-annotations 11.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,244 kB
  • sloc: cpp: 21,698; makefile: 1,505; ansic: 165; sh: 121; perl: 90
file content (68 lines) | stat: -rw-r--r-- 2,207 bytes parent folder | download | duplicates (7)
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
    // variadic function template
    template<typename ...Params>
    void fun(Params ...parameters)
    {}

    // instantiation declaration
    template
    void fun<int>(int v);

    // pointers to functions that are instantiated from a variadic function
    // template
    void (*fp)(int) = fun;
    void (*fp2)(int, double) = fun;

    // template class having variadic function template member
    template <typename T>
    struct Demo
    {
        template<typename ...Params>
        void mem(Params ...parameters)
        {}
    };

    // instantiation declaration:
    template
    void Demo<char>::mem<int>(int v);

    // pointers to functions that are instantiated from a variadic function
    // template
    void (Demo<char>::*mp)(int) = &Demo<char>::mem;
    void (Demo<char>::*mp2)(int, double) = &Demo<char>::mem;


    // explicitly obtain the address of the Demo::mem member expecting an int:
    void (Demo<char>::*mp1)(int) =
                    //&Demo<char>::mem(int);
        // error meesage: expected primary-expression before 'int'
                    &Demo<char>::mem<int>;
        // OK


    // a template class using perfect forwarding:
    template<typename T>
    struct Demo2
    {
        template<typename ...Args>
        void mem(Args&&...args)
        {}
    };

    // instantiation declaration:
    template
        // void Demo2<char>::mem<int>(int v);
        // error: template-id 'mem<int>' for 'void Demo2<char>::mem(int)' does
        //        not match any template declaration
        // void Demo2<char>::mem<typename ...Args>(Args &&...args);
        // error: using 'typename' outside of template
        //        expected parameter pack before '...'
        //        parse error in template argument list
        //        template-id 'mem<<expression error> >' used as a declarator
        //        variable or field 'mem' declared void
        //        expected `;' before '(' token
    void Demo2<char>::mem<int &&>(int &&);
        // or:  void Demo2<char>::mem(int &&);

    // pointers to Demo2::mem:
//OK    void (Demo2<char>::*mpd2)(int &&) = &Demo2<char>::mem;
    void (Demo2<char>::*mpd2)(int &&) = &Demo2<char>::mem<int>; // ook OK