C++ Library Extensions 2022.12.09
To help learn modern C++ programming
025-template_members.cpp
Go to the documentation of this file.
1#include <iostream>
2
3/*
4 Partial Type:
5
6 Types that are declared but not defined yet.
7
8 For example,
9
10 template<typename ElementTypeA> class TypeA;
11
12 int a[]; // declaration, but we have not defined the element count of array of type int.
13
14 So, a is still a partial type.
15
16 In C++, type void is also regarded as a partial type.
17
18 In C++, name should be declared before its use.
19
20 In C++, template members that are not used are not instantiated.
21
22 When the template member is complex such that C++ compiler fails to parse
23 the syntax properly, in such case, you have to prepend keyword template
24 in front of the member template as below.
25*/
26
27// this is partial declaration, the type of TypeB
28// is not yet defined
29template<typename ElementTypeB> class TypeB;
30
31template<typename ElementTypeA> // template clause
32class TypeA
33{
34 template<typename ElementType> friend class TypeB;
35
36 private:
37 ElementTypeA m_data{};
38
39 public:
40 TypeA(ElementTypeA v = ElementTypeA{}):
41 m_data{v} { }
42
43 // in-class template member definition
44 template<typename ElementType>
46 {
47 std::cout << b.m_data << std::endl;
48 }
49
50 // member template, declared but not defined
51 template<typename Type> auto sum(Type a, Type b);
52
53 // in-class static member function definition
54 template<typename Type, typename... Types>
55 static auto summation(Type first, Types... args)
56 {
57 return (first + ... + args);
58 }
59
60};
61
62template<typename ElementTypeB> // template clause
63class TypeB
64{
65 // this is syntax error
66 // keyword friend should not come before template clause
67 // friend template<typename ElementType> class TypeA;
68 template<typename ElementType> friend class TypeA;
69
70 private:
71 ElementTypeB m_data{};
72
73 public:
74 TypeB(ElementTypeB v = ElementTypeB{} ): m_data { v }
75 { }
76
77 // in-class template member definition
78 template<typename ElementType>
80 {
81 std::cout << a.m_data << std::endl;
82 }
83
84 // static member declaration
85 template<typename Type, typename... Types>
86 static auto products(Type first, Types... args);
87};
88
89template<typename Type>
91{
92 Type m_data;
93};
94
95// out-of-class definition of TypeA<ElementTypeA>::sum(Type, Type)
96template<typename ElementTypeA>
97 template<typename Type>
98 auto TypeA<ElementTypeA>::sum(Type a, Type b)
99{
100 return element_t<Type>{ a + b };
101}
102
103// out-of-class static member function definition
104template<typename ElementType>
105 template<typename Type, typename... Types>
106 auto TypeB<ElementType>::products(Type first, Types... args)
107 {
108 return (first * ... * args);
109 }
110
112{
113 TypeA<int> a;
114
115 TypeB<int> b;
116
118
120}
121
123{
124 TypeA<int> a;
125
126 auto rlt = a.sum(4, 6);
127
128 std::cout << "sum of 4 and 6 = " << rlt.m_data << std::endl;
129
130}
131
133{
134 auto summation = TypeA<double>::template summation(1, 2, 3, 4, 5);
135 auto product = TypeB<int>::template products(1, 2, 3, 4, 5);
136
137 std::cout << "summation 1 to 5 = " << summation << std::endl;
138 std::cout << "product 1 to 5 = " << product << std::endl;
139}
140
141int main()
142{
143 // test_template_non_static_member_function();
144 // test_out_of_class_definition();
146}
int summation(int a, int b)
void test_template_non_static_member_function()
void test_out_of_class_static_member_functions()
void test_out_of_class_definition()
int main()
auto & cout
auto & endl
void operation_over_type_b(TypeB< ElementType > b)
static auto summation(Type first, Types... args)
auto sum(Type a, Type b)
void operation_over_type_b(TypeB< ElementTypeB > b)
TypeA(ElementTypeA v=ElementTypeA{})
void operation_over_type_a(TypeA< ElementType > a)
TypeB(ElementTypeB v=ElementTypeB{})
void operation_over_type_a(TypeA< ElementTypeA > a)
static auto products(Type first, Types... args)
auto product(std::vector< ElementType > const &A, std::vector< ElementType > const &B, VectorTypes... tails)