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;        // OK 
4. 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" )


版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/aglanceofunknown/archive/2013/04/27/4612084.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值