1 // C++ examples and requests from Klaus Berndl
3 // template in a unnamed namespace
6 template<typename Target, typename Source>
7 Target lexical_cast(Source arg)
9 std::stringstream interpreter;
12 if(!(interpreter << arg) || !(interpreter >> result) ||
13 !(interpreter >> std::ws).eof())
14 throw bad_lexical_cast();
21 template <class T, FOO> class Vector
28 T& elem(int i) {return v[i];}
35 template <> class Vector <void*>
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];}
50 // template contains namespace
51 typedef vector<CzkUtilities::Action*> ActionList;
53 // declaration of some template-types
54 map<XXX, Test::YYY>** map_var;
56 map_with_size<XXX, Test::YYY, size> map_size_var;
57 typedef map_with_size<XXX, Test::YYY, size> SizedMap;
59 map_with_10_size<XXX, Test::YYY, 10>* pMap_size10_var;
60 typedef map_with_10_size<XXX, Test::YYY, 10> Size10Map;
62 // a function which such a template-argument
63 void* test_function(map<ClassX, Test::ClassY, 10>* pMap);
66 template <class T> class Vector <T*> : private Vector <void*>
69 typedef Vector <void*> Base;
73 T*& elem(int i) {return static_cast<T*&>(Base::elem(i));}
77 // outside method implementation of a template-class
78 template<class T> T& Vector<T*>::elem(int i)
83 // same but qualified with a namespace Testnamespace
84 template<class T> T& Testnamespace::Vector<T*>::elem(int i)
89 // function templates with keyword typename
90 template<typename Target, typename Source>
91 Target lexical_cast(Source arg)
93 std::stringstream interpreter;
96 if(!(interpreter << arg) || !(interpreter >> result) ||
97 !(interpreter >> std::ws).eof())
98 throw bad_lexical_cast();
105 typename T::_ptr_type
106 getService(const std::string& pServiceName, const int pRetries=20)
108 return T::_narrow(getServiceObject(pServiceName, pRetries));
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)
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;