Debian Codegraph @

yo/advancedtemplates/examples/variadic.cc

raw
1 // variadic function template
2 template<typename ...Params>
3 void fun(Params ...parameters)
4 {}
5
6 // instantiation declaration
7 template
8 void fun<int>(int v);
9
10 // pointers to functions that are instantiated from a variadic function
11 // template
12 void (*fp)(int) = fun;
13 void (*fp2)(int, double) = fun;
14
15 // template class having variadic function template member
16 template <typename T>
17 struct Demo
18 {
19 template<typename ...Params>
20 void mem(Params ...parameters)
21 {}
22 };
23
24 // instantiation declaration:
25 template
26 void Demo<char>::mem<int>(int v);
27
28 // pointers to functions that are instantiated from a variadic function
29 // template
30 void (Demo<char>::*mp)(int) = &Demo<char>::mem;
31 void (Demo<char>::*mp2)(int, double) = &Demo<char>::mem;
32
33
34 // explicitly obtain the address of the Demo::mem member expecting an int:
35 void (Demo<char>::*mp1)(int) =
36 //&Demo<char>::mem(int);
37 // error meesage: expected primary-expression before 'int'
38 &Demo<char>::mem<int>;
39 // OK
40
41
42 // a template class using perfect forwarding:
43 template<typename T>
44 struct Demo2
45 {
46 template<typename ...Args>
47 void mem(Args&&...args)
48 {}
49 };
50
51 // instantiation declaration:
52 template
53 // void Demo2<char>::mem<int>(int v);
54 // error: template-id 'mem<int>' for 'void Demo2<char>::mem(int)' does
55 // not match any template declaration
56 // void Demo2<char>::mem<typename ...Args>(Args &&...args);
57 // error: using 'typename' outside of template
58 // expected parameter pack before '...'
59 // parse error in template argument list
60 // template-id 'mem<<expression error> >' used as a declarator
61 // variable or field 'mem' declared void
62 // expected `;' before '(' token
63 void Demo2<char>::mem<int &&>(int &&);
64 // or: void Demo2<char>::mem(int &&);
65
66 // pointers to Demo2::mem:
67 //OK void (Demo2<char>::*mpd2)(int &&) = &Demo2<char>::mem;
68 void (Demo2<char>::*mpd2)(int &&) = &Demo2<char>::mem<int>; // ook OK