C++ Template learning notes
1. Function Template
template< typename T1, typename T2, typename T3> T1 max( T2& a, T3& b) { ... } // explicit call ::max<int, int, double>(1, 2.3); //or ::max<int>(1, 2.3); // return type is int // string literals as argument template <typename T> void ref (T const& x) { std::cout << "x in ref(T const&): " << typeid(x).name() << '\n'; } template <typename T> void nonref (T x) { std::cout << "x in nonref(T): " << typeid(x).name() << '\n'; } // out in gcc 3.4.4 // x in ref(T const&): A6_c // x in nonref(T): PKc
notice: template argument deduction does not match up return types.
模板函数可以做重载,编译器优先调用非模板函数(函数的定义在调用之前),可 ::max<>(1, 2) 进行显示的模板函数调用
default template arguments may not be used in function templates
Template template parameters for function templates are not allowed.
Template can not be declared in a Function.
2. Class Template
// template class template<typename T> class Stack { T member; public: T foo(T a); template<typename T2> T& operator=(Stack<T2> const& other); }; template<typename T> T Stack<T>::foo(T a) { return a; } template<typename T> template<typename T2> T& Stack<T>::operator=(Stack<T2> const& other) { return *(this); } // specializing class templates // allows you to optimize implementations for certain types or // to fix a misbehavior of certain types for an instantiation of the class template template<> class Stack<std::string>{ std::string m1; std::string m2; public: void foo(std::string& a); }; void Stack<std::string>::foo(std::string& a) {} // Partial Specialization template<typename T> class Stack<T*>{ public: T* foo(T* a); }; template<typename T> T* Stack<T*>::foo(T* a) { return a; } // Template template parameters template<typename T, template<typename ET> class CONT=std::deque > class Stack { CONT<T> data; public: T foo(T a); }; template<typename T, template<typename ET> class CONT> T Stack<T, CONT>::foo(T a) { return a; }
notice: for class templates, only those member functions that are called are instantiated.
Member function cannot be declared virtual.
3. Restrictions for Nontype Template Parameters
nontype template parameters may be constant integral values (including enumerations) or pointers to objects with external linkage.
Floating-point numbers and class-type objects are not allowed as nontype template parameters
template <double VAT> // ERROR: floating-point values are not double process (double v) // allowed as template parameters { return v * VAT; } template <std::string name> // ERROR: class-type objects are not class MyClass { // allowed as template parameters … }; template <char const* name> class MyClass { … }; MyClass<"hello"> x; // ERROR: string literal "hello" not allowed char const* s = "hello"; MyClass<s> x; // ERROR: s is pointer to object with internal linkage extern char const s[] = "hello"; MyClass<s> x; // OK4. The keyword typename was introduced during the standardization of C++ to clarify that an identifier inside a template is a type.
// print elements of an STL container template <typename T> void printcoll (T const& coll) { typename T::const_iterator pos; // iterator to iterate over coll typename T::const_iterator end(coll.end()); // end position for (pos=coll.begin(); pos!=end; ++pos) { std::cout << *pos << ' '; } std::cout << std::endl; }5. The .template Construct
template<int N> void printBitset(std::bitset<N> const& bs) { std::cout << bs.template to_string<char, char_traits<char>, allocator<char> >(); } // Without that extra use of template, the compiler does not know that the less-than // token (<) that follows is not really "less than" but the beginning of a template // argument list. Note that this is a problem only if the construct before the period // depends on a template parameter. In our example, the parameter bs depends // on the template parameter N.6. Template name cannot have C linkage ( extern "C" )
版权声明:本文为博主原创文章,未经博主允许不得转载。