C++ Template learning notes
2013-04-27 17:11
447 查看
1. Function Template
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
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< 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" )
相关文章推荐
- C++ Template learning notes
- C++ learning notes - Function
- Data structures for game programing learning notes (一)—— template
- 1.2 Learning C++ [notes.learn]
- C++ learning notes - const
- C++ - 函数模板(function template)右值引用参数 问题 及 解决
- c++ template(5)模板实战
- Learning a Part of C++(for ACM/ICPC) (2) 类的封装
- C++ - 函数模板(function template) 的 重载(overload) 详解 及 代码
- 【Machine Learning notes(机器学习笔记-001)】
- 2017 程序设计实习之C++部分作业题汇总 - F:模板 template
- C++ template —— tuple(十三)
- C++ Template 中的typename、class关键字
- C++ Template
- C++ - 可变参数函数模板(Variadic Function Template) 详解 及 代码
- C++ Note-Template-01
- c++模版 template
- JS learning notes
- Machine Learning Notes - PLA
- C++ -- variadic template (可变参数模板)