104494
|
1 // C++ examples and requests from Klaus Berndl
|
|
2
|
|
3 // template in a unnamed namespace
|
|
4 namespace
|
|
5 {
|
|
6 template<typename Target, typename Source>
|
|
7 Target lexical_cast(Source arg)
|
|
8 {
|
|
9 std::stringstream interpreter;
|
|
10 Target result;
|
|
11
|
|
12 if(!(interpreter << arg) || !(interpreter >> result) ||
|
|
13 !(interpreter >> std::ws).eof())
|
|
14 throw bad_lexical_cast();
|
|
15
|
|
16 return result;
|
|
17 }
|
|
18 }
|
|
19
|
|
20
|
|
21 template <class T, FOO> class Vector
|
|
22 {
|
|
23 private:
|
|
24 static T* v;
|
|
25 int sz;
|
|
26
|
|
27 public:
|
|
28 T& elem(int i) {return v[i];}
|
|
29 virtual ~Vector ();
|
|
30
|
|
31 protected:
|
|
32 Vector ();
|
|
33 };
|
|
34
|
|
35 template <> class Vector <void*>
|
|
36 {
|
|
37 private:
|
|
38 void** v;
|
|
39 int sz;
|
|
40
|
|
41 public:
|
|
42 Vector ();
|
|
43 virtual int func1(int i);
|
|
44 virtual int func2(int i) = 0;
|
|
45 static virtual int func3(int i) = 0;
|
|
46 void*& elem(int i) {return v[i];}
|
|
47 //...
|
|
48 };
|
|
49
|
|
50 // template contains namespace
|
|
51 typedef vector<CzkUtilities::Action*> ActionList;
|
|
52
|
|
53 // declaration of some template-types
|
|
54 map<XXX, Test::YYY>** map_var;
|
|
55
|
|
56 map_with_size<XXX, Test::YYY, size> map_size_var;
|
|
57 typedef map_with_size<XXX, Test::YYY, size> SizedMap;
|
|
58
|
|
59 map_with_10_size<XXX, Test::YYY, 10>* pMap_size10_var;
|
|
60 typedef map_with_10_size<XXX, Test::YYY, 10> Size10Map;
|
|
61
|
|
62 // a function which such a template-argument
|
|
63 void* test_function(map<ClassX, Test::ClassY, 10>* pMap);
|
|
64
|
|
65
|
|
66 template <class T> class Vector <T*> : private Vector <void*>
|
|
67 {
|
|
68 public:
|
|
69 typedef Vector <void*> Base;
|
|
70
|
|
71 Vector () : Base() {}
|
|
72
|
|
73 T*& elem(int i) {return static_cast<T*&>(Base::elem(i));}
|
|
74 //...
|
|
75 };
|
|
76
|
|
77 // outside method implementation of a template-class
|
|
78 template<class T> T& Vector<T*>::elem(int i)
|
|
79 {
|
|
80 return C;
|
|
81 }
|
|
82
|
|
83 // same but qualified with a namespace Testnamespace
|
|
84 template<class T> T& Testnamespace::Vector<T*>::elem(int i)
|
|
85 {
|
|
86 return C;
|
|
87 }
|
|
88
|
|
89 // function templates with keyword typename
|
|
90 template<typename Target, typename Source>
|
|
91 Target lexical_cast(Source arg)
|
|
92 {
|
|
93 std::stringstream interpreter;
|
|
94 Target result;
|
|
95
|
|
96 if(!(interpreter << arg) || !(interpreter >> result) ||
|
|
97 !(interpreter >> std::ws).eof())
|
|
98 throw bad_lexical_cast();
|
|
99
|
|
100 return result;
|
|
101 }
|
|
102
|
|
103 template<class T>
|
|
104 static
|
|
105 typename T::_ptr_type
|
|
106 getService(const std::string& pServiceName, const int pRetries=20)
|
|
107 {
|
|
108 return T::_narrow(getServiceObject(pServiceName, pRetries));
|
|
109 }
|
|
110
|
|
111 // function template declaration
|
|
112 template<class T> void sort(vector<T>&);
|
|
113 // complex function template definition
|
|
114 template<class T, class S, const NamespaceY::ClassX<TestClass, &s> volatile ***&i>
|
|
115 map<ClassY, ClassX, 10>
|
|
116 sort(const vector<T>& v)
|
|
117 {
|
|
118 return;
|
|
119 }
|
|
120
|
|
121 // variable declarations of template-types
|
|
122 foo<TClass, Testnamespace::XClass, i> *bar1;
|
|
123 foo<TClass, Testnamespace::XClass, **&i> *bar2;
|
|
124 foo<TClass, Testnamespace::XClass, *Namespace::ClassX::i> bar3;
|
|
125 foo<0> bar0;
|
|
126
|
|
127 class SomeName;
|
|
128 class OtherName;
|
|
129
|
105377
|
130 // arch-tag: 55ff74de-74dc-44ad-8252-50dc5f3492c3
|