第16章 模板与泛型编程

16.1.1函数模板

//template parameter list
template<typename T1,typename T2>
int compare(const T1& v1, const T2&v2)
{
    if (v1 < v2) return -1;
    if (v2 < v1) return 1;
    return 0;
}

When we call a function template, the compiler (ordinarily) uses the arguments of the call to deduce the template parameter(s) for us.

The compiler uses the deduced template parameter(s) to instantiatea specific version of the function for us.

模板类型参数

函数模板的参数列表必须明确标明typename或者class,两个是同义的(typename在模板引入C++之后才出现,很多早先的程序使用class)

非类型模板参数

// 整数
template<unsigned N, unsigned M>
int compare(const char(&p1)[N], const char(&p2)[M])
{
    strcpy(p1,p2);
}
// 指针
template<const char* C>
void func1(const char* str)
{
    cout << C << " " << str << endl;
}

// 引用
template<char(&R)[9]>
void func2(const char* str)
{
    cout << R << " " << str << endl;
}

// 函数指针
template<void(*f)(const char*)>
void func3(const char* c)
{
    f(c);
}
void print(const char* c) { cout << c << endl; }
char arr[9] = "template";   // 全局变量,具有静态生存期
int main()
{
    func1<arr>("pointer");
    func2<arr>("reference");
    func3<print>("template function pointer");
    return 0;
}

绑定到非类型整型的实参必须是一个常量表达式,绑定到指针或引用的非类型参数的实参必须有静态的生存期(比如全局变量)

inline和constexpr的函数模板

template<typename T1, typename T2> inline int compare(const T1& v1, const T2&v2);
template<typename T1, typename T2> constexpr int compare(const T1& v1, const T2&v2);

对于函数模板,编译器可以通过参数推断,得出模板中的类型信息,因而在使用函数模板时可以省略<typeName>。

16.1.2类模板

对于函数模板,编译器可以通过参数推断,得出模板中的类型信息,因而在使用函数模板时可以省略<typeName>。对于类模板,必须在模板后的尖括号中提供信息,代替模板参数实参列表。

  1. 类模板中,模板参数被被当做stand-ins(替身),使用时被替换
  2. 类模板中的成员函数只有在使用时才会被实例化,这使的某种类型即使不完全符合模板要求,仍然可以使用该类型的模板。
  3. 实例化后的类型为ClassName<typeName>类型。
  4. 类包含友元时,模板类包含非模板友元,则友元可以访问模板的所有类型实例;友元是模板,则所有的友元实例都可以访问模板类的所有实例。
  5. 对于static成员,每一个不同类型实例,都有不同的static成员,相同类型的实例实例化出来的对象共享static成员。
#include<memory>
#include <vector>
using namespace std;

template <typename> class BlobPtr;// forward declarations needed for friend declarations in Blob
template <typename> class Blob; // needed for parameters in operator==

template <typename T>
bool operator==(const Blob<T>&, const Blob<T>&);
template<typename T> class Blob
{
    // each instantiation of Blob grants access to the version of
    // BlobPtr and the equality operator instantiated with the same type
    friend class BlobPtr<T>;
    friend bool operator==<T> (const Blob<T>&, const Blob<T>&);
public:
    using size_type = vector<T>::size_type;
    //construct function
    Blob():data(make_shared<vector<T>>()) { };
    Blob(std::initializer_list<T> il):data(make_shared<vector<T>>(il)) { }
    //number of elements in Blob
    size_type size() const { return data->size(); }
    bool empty() const { return data->empty(); }
    //add and remove member
    void push_back(const T &t) { data->push_back(t); }
    void push_back(T &&t) { data->push_back(std::move(t)); }
    void pop_back() 
    {
        check(0, "pop_back on empty StrBlob");
        data->pop_back();
    }
    //element access
    T& back()
    {
        check(0, "back on empty StrBlob");
        return data->back();
    }
    T& operator[](size_type i) {
        check(i, "out of range!"); 
        return (*data)[i]; }
private:
    std::shared_ptr<std::vector<T>> data;
    void check(size_type i, const std::string &msg) const;
};
//在类外必须使用实例化之后的类型Blob<T>
template<typename T>
void Blob<T>::check(size_type i, const std::string & msg) const
{
    if (i >= data->size())
        throwout_of_range(msg);
}
template <typename T>
bool operator==(const Blob<T> lhs, const Blob<T> rhs)
{
    if (rhs.size() != lhs.size())
        return false;
    for (size_t i = 0; i < lhs.size(); ++i) {
        if (lhs[i] != rhs[i])
            return false;
    }
    return true;
}

// BlobPtr throws an exception on attempts to access a nonexistent element 
template <typename T>
bool operator==(const BlobPtr<T>&, const BlobPtr<T>&);
template <typename T> class BlobPtr : public std::iterator<std::bidirectional_iterator_tag, T> 
{
    friend bool operator==<T>(const BlobPtr<T>&, const BlobPtr<T>&);
public:
    BlobPtr() : curr(0) { }
    BlobPtr(Blob<T> &a, size_t sz = 0) : wptr(a.data), curr(sz) { }

    T &operator[](std::size_t i)
    {
        auto p = check(i, "subscript out of range");
        return (*p)[i];  // (*p) is the vector to which this object points
    }

    const T &operator[](std::size_t i) const
    {
        auto p = check(i, "subscript out of range");
        return (*p)[i];  // (*p) is the vector to which this object points
    }

    T& operator*() const
    {
        auto p = check(curr, "dereference past end");
        return (*p)[curr];  // (*p) is the vector to which this object points
    }
    T* operator->() const
    { // delegate the real work to the dereference operator
        return &this->operator*();
    }

    // increment and decrement
    // postfix operators
    BlobPtr operator++(int)
    {
        // no check needed here; the call to prefix increment will do the check
        BlobPtr ret = *this;   // save the current value
        ++*this;     // advance one element; prefix ++ checks the increment
        return ret;  // return the saved state
    }
    BlobPtr operator--(int)
    {
        // no check needed here; the call to prefix decrement will do the check
        BlobPtr ret = *this;  // save the current value
        --*this;     // move backward one element; prefix -- checks the decrement
        return ret;  // return the saved state
    }
    BlobPtr& operator++()
    {
        // if curr already points past the end of the container, can't increment it
        check(curr, "increment past end of BlobPtr");
        ++curr;       // advance the current state
        return *this;
    }
    BlobPtr& BlobPtr<T>::operator--()
    {
        // if curr is zero, decrementing it will yield an invalid subscript
        --curr;       // move the current state back one element
        check(-1, "decrement past begin of BlobPtr");
        return *this;
    }

private:
    // check returns a shared_ptr to the vector if the check succeeds
    std::shared_ptr<std::vector<T>> check(std::size_t i, const std::string &msg) const
    {
        auto ret = wptr.lock();   // is the vector still around?
        if (!ret)
            throw std::runtime_error("unbound BlobPtr");
        if (i >= ret->size())
            throw std::out_of_range(msg);
        return ret; // otherwise, return a shared_ptr to the vector
    }

    // store a weak_ptr, which means the underlying vector might be destroyed
    std::weak_ptr<std::vector<T>> wptr;
    std::size_t curr;      // current position within the array
};
// equality operators
template <typename T>
bool operator==(const BlobPtr<T> &lhs, const BlobPtr<T> &rhs)
{
    return lhs.wptr.lock().get() == rhs.wptr.lock().get() &&
        lhs.curr == rhs.curr;
}
template <typename T>
bool operator!=(const BlobPtr<T> &lhs, const BlobPtr<T> &rhs)
{
    return !(lhs == rhs);
}
类模板示例

16.1.3模板参数

模板参数的作用域在声明之后定义结束之前。

模板参数可以覆盖外部作用域的类型参数。

模板参数可以有默认值

使用类的类型成员时需要使用typename关键字,如typename T::value_type()

//默认模板参数
template<typename T=int>
//使用参数类型的类型成员作为返回值的类型
typename vector<T>::size_type differ(vector<T>& lh, vector<T>& rh)
{
    return lh.size() - rh.size();
}

16.1.4类的成员也可以是模板

普通类中的成员可以为模板

模板类中的成员可以为模板

//为了在unique_ptr析构对象的时候打印出来信息
class DebugDelete
{
public:
    DebugDelete(std::ostream&s = std::cerr) :os(s) {}
    template <typename T> void operator()(T*p)const
    {
        os << "deleting unique_ptr" << std::endl;
        delete p;
    }
private:
    std::ostream &os;
};
int main()
{
    //会生成一个int版本的DebugDelete实例
    unique_ptr<int, DebugDelete> p(new int, DebugDelete());
    return 0;
}

16.1.5控制实例化

模板被使用的时候才会实例化,其实例可能出现在多个对象文件中,当独立编译的原文件使用相同的模板并提供了相同的模板参数,每个文件都会有一个实例。为了解决这个开销,可以使用显式实例化。

extern template declaration; //实例化的声明

template declaration;//实例化的定义

编译器遇到external声明,将会在其他位置寻找定义,但在其他位置必须有一个定义。

实例化定义会实例化所有成员,所以类型必须能够引用所有成员函数

16.2模板实参推断

  1. 在调用模板的时候,一般不会发生参数类型的转换。会发生的转换有两种,非const转换成const,数组或函数名转换成指针。
  2. 如果函数参数不是模板参数类型,这个参数是可以进行类型转换的
  3. 使用相同模板参数类型的函数形参类型必须相同,不会进行参数转换
  4. 当模板实参类型无法推断,例如返回值类型时,必须显式指定。(通过fun<typename, …>)
  5. 显式指定了模板实参类型的,也可以进行类型转换(fun<double>(15),15转换为double)。

16.2.3尾置返回类型与类型转换

有时候返回值的类型与参数的类型有关,这时候可以用尾置返回类型

template <typename It>
auto fcn(It beg, It end) -> decltype(*beg)
{
    return *beg;
}

由于解引用运算返回的是引用类型,所以返回值是引用类型。

如果要返回的是值,则使用remove_reference(是一个转换模板,定义在type_traits头文件中)。

template <typename It>  //第二个typename表示type成员为类型
auto fcn(It beg, It end) -> typename remove_reference<decltype(*beg)>::type
{
    return *beg;
}

其他的类型转换模板请参见这里

16.2.6理解std::move

Std::move将一个对象转变为右值引用对象。

template<class _Ty> inline
constexpr typename remove_reference<_Ty>::type&&
move(_Ty&& _Arg) noexcept
{    // forward _Arg as movable
    return (static_cast<typename remove_reference<_Ty>::type&&>(_Arg));
}

在调用时

std::string s1("hi!"), s2;
s2 = std::move(std::string("bye!"));
s2 = std::move(s1);

使用调用

s2 = std::move(std::string("bye!"));
  1. 推断出模板类型_Ty为string
  2. remove_reference<_Ty>::type为string
  3. move返回类型为string&& 

使用调用

s2 = std::move(s1);
  1. 推断出模板类型_Ty为string&
  2. remove_reference<_Ty>::type为string
  3. move返回类型为string& &&,折叠后为string&
  4. move函数参数t实例化为string& &&,会折叠为string&

16.2.7转发

某些函数需要将其一个或多个实参和类型不变地转发给其他函数,包括是否是const以及左值还是右值。

这个模板将一个函数的参数转发到里边的函数。

template<typename F,typename T1,typename T2>
void flip1(F f, T1 t1, T2, t2)
{
    f(t2, t1);
}

这个模板在值传递的实例中是正确的,但是在如下调用:

function<void(int, int&)> fun = [](int a, int& b) {cout << a << ++b << endl; };
int a = 1, b = 2;
flip1(fun, a, b);
cout << a << b << endl;

你会发现,函数并没有改变第二个参数的值,没有将引用这个参数类型传入。

修改模板为

template<typename F,typename T1,typename T2>
void flip1(F f, T1&& t1, T2&& t2)
{
    f(t1, t2);
}

这也会存在一些情况下的错误

function<void(int&&, int&)> fun = [](int &&a, int& b) {cout << a << ++b << endl; };
int a = 1;
flip1(fun, 1, a);

1为右值传递到模板flip1中后,作为一个变量的形参,会成为一个左值(变量是左值),左值绑定到fun的第一个右值参数时,会出现错误。

再次修改为

template<typename F,typename T1,typename T2>
void flip1(F f, T1&& t1, T2&& t2)
{
    f(std::forward<T1>(t1), std::forward<T2>(t2));
}

使用forward,将返回类型为T&&的结果,如果出入为值,则返回为右值,传入为左值引用,则引用折叠后返回左值引用。

16.3重载与模板

如下两个模板

template<typename T> std::string debug_rep(const T& t)
{
    std::ostringstream ret;
    ret << t;
    return ret.str();
}
template<typename T> std::string debug_rep(T* p)
{
    std::ostringstream ret;
    ret << "pointer: " << p << " " << (p ? debug_rep(*p) : "null pointer");
    return ret.str();
}

如果有调用

int a = 1;
std::cout << debug_rep(a) << std::endl;

只有第一个模板时可行的,则选择第一个实例化

如果有调用

int a = 1;
std::cout << debug_rep(&a) << std::endl;

则有

debug_rep(const int*&)
debug_rep(int*)

这两个版本的实例化,由于第一个需要有非const转换到const,所以选择了第二个版本。

如果有调用

const int* a = &b;
std::cout << debug_rep(a) << std::endl;

则上述两个版本都是精确的,然而debug_rep(T* p)对于这个调用来讲,是更特例的版本,只针对于指针,所以选择最特例话的版本。(从另一个角度讲,(const T& t)适用任何类型,这样debug_rep(T* p)永远不会被使用,这条规则才能使其被使用)

另外,非模板函数会优先匹配

16.4可变参数模板

一个可变参数模板(variadic template)中可变数目的参数被称为参数包(parameter packet),分为两种参数包:模板参数包(template parameter packet)、函数参数包(function parameter)。

// Args is a template parameter pack; rest is a function parameter pack
// Args represents zero or more template type parameters
// rest represents zero or more function parameters
template <typename T, typename... Args>
void foo(const T &t, const Args& ... rest)
{
    cout << sizeof...(Args) << endl;  // number of type parameters
    cout << sizeof...(args) << endl;  // number of function parameters
}

用typename…表示零或多个类型的列表,一个类型名后边跟省略号,表示非类型参数的列表。如果一个函数参数的类型是模板参数包,则此参数是一个函数参数包,可以用sizeof…求出参数的个数。

16.4.1例子,打印所有参数

第一个模板在第二个模板递归调用的最后一次中,时最为匹配的,调用时终止了第二个模板的递归。

// function to end the recursion and print the last element
// this function must be declared before the variadic version of print is defined
template<typename T>
ostream &print(ostream &os, const T &t)
{
    return os << t; // no separator after the last element in the pack
}
// this version of print will be called for all but the last element in the pack
template <typename T, typename... Args>
ostream &print(ostream &os, const T &t, const Args&... rest)
{
    os << t << ", ";  // print the first argument
    return print(os, rest...); // recursive call; print the other arguments
}

16.4.2包扩展

template <typename T, typename... Args>
ostream &print(ostream &os, const T &t, const Args&... rest) //扩展Args,生成参数列表
{
    os << t << ", ";  
    return print(os, rest...); //扩展实参,形成实参列表
}

在扩展实参的时候,还可以这样用

print(os, debug_rep(rest)...);

可以将扩展出来的每一个参数,执行函数调用

return print(os, debug_rep(p1), debug_rep(p2), debug_rep(p3));

16.4.3转发参数包

// fun haszero or more parameters each of which is
// an rvalue reference to a template parameter type
template<typename... Args>
void fun(Args&&... args) // expands Args as a list of rvalue references
{
    // the argument to work expands both Args and args
    work(std::forward<Args>(args)...);
}

16.5模板特例化

// firstversion; can compare any two types
template <typename T> int compare(const T&, const T&);
// second version to handle string literals
template<size_t N, size_t M>
int compare(const char(&)[N], const char(&)[M]);

定义的这两个模板,可以比较任意对象类型。但是,如果传入的是字符串类型的指针(char*)时,回到第一个模板中调用,然而比较了参数指针的大小。

为了能够处理字符指针的类型,可以定义一个特列

// specialversion of compare to handle pointers to character arrays
template <>
int compare(const char* const &p1, const char* const &p2)
{
    return strcmp(p1, p2);
}

当提供一个特例化,其类型必须与之前的模板参数中对应的类型匹配。如上是为了特例化第一个模板template <typename T> int compare(const T&, const T&);了,其参数是顶层const的,而对于指针类型char*,其参数是底层const,所以在特例化的时候为const char* const &p1。

需要注意的是,特例化本质是实例化一个模板,而非重载。因此特例化不影响函数匹配。这与将其定义为非模板函数是有区别的。

加入定义了非模板函数,这样在传入字符常量的时候,例如

compare("hi", "mom");

会匹配到我们定义的非模板函数,而这个实际上我们希望匹配到int compare(const char(&)[N], const char(&)[M]); 

注意,特例化必须声明在作用域中,故声明在一个头文件中

类模板的特例化

如果自定义类需要使用无序容器,必须定义自己的hasher版本,或者特例化hash<key_type>版本,一个特例化的hash类必须定义:

  1. 一个重载调用运算符,接收一个容器关键字类型的对象,返回一个size_t
  2. 两个类型成员,result_type和argument_type,分别为调用运算符的返回类型和参数类型
  3. 默认构造函数和拷贝赋值运算符(可以隐式定义)。

注意,特例化需要与被特例化的模板在同一个作用域,所以使用namespace参数。

示例:对Sales_data

// openthe std namespace so we can specialize std::hash
namespace std 
{
    template <>  // we're defining a specialization with
    struct hash<Sales_data> // the template parameter of Sales_data
    {
        // the type used to hash an unordered container must define these types
        typedef size_t result_type;
        typedef Sales_data argument_type; // by default, this type needs ==
        size_t operator()(const Sales_data& s) const;
        // our class uses synthesized copy control and default constructor
    };
    size_t hash<Sales_data>::operator()(const Sales_data& s) const
    {
        //下边是hash函数的算法,使用了string、uint、double的hash算法
        return hash<string>()(s.bookNo) ^
            hash<unsigned>()(s.units_sold) ^
            hash<double>()(s.revenue);
    }
}

注意,有时候会使用私有成员进行hash,所以声明为友元

// needed for the friend declaration
template <classT> class std::hash;  
class Sales_data {
    friend class std::hash<Sales_data>;
    // other members as before
};

类模板的部分特例化

我们可以部分特例化类模板,不能部分特例化函数模板

template <class T> struct remove_reference {
    typedef T type;
};
// partialspecializations that will be used for lvalue and rvalue references
template <class T> struct remove_reference<T&>  // lvalue references
{
    typedef T type;
};
template <class T> struct remove_reference<T&&> // rvalue references
{
    typedef T type;
};

特例化模板的成员

template <typename T> struct Foo {
    Foo(constT &t = T()) : mem(t) { }
    void Bar() { /* ...*/ }
    T mem;
    // other members of Foo
};
template<>  // we're specializing a template
void Foo<int>::Bar() // we're specializing the Bar member of Foo<int>
{
    // do whatever specialized processing that applies to ints
}

当使用int类型的模板时,会使用这个特例化的Bar()函数成员。

16.6 SFINAE

SFINAE是Substitution Failure Is Not An Error的缩略语

#include <iostream>

// return type `bool` is only valid if T is an integral type
template <class T>
typename std::enable_if<std::is_integral<T>::value, bool>::type  is_odd (T i)
{
    return bool(i % 2);
}
// template argument is only valid if T is an integral type
template <class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
bool is_even( T i)
{
    return !bool(i % 2);
}

int main()
{
    std::cout << is_odd(123) << std::endl;
    std::cout << is_even(123) << std::endl;
    std::cout << is_odd(123.0) << std::endl; // compile error
    std::cout << is_even(123.0) << std::endl; // compile error
    return 0;
}
原文地址:https://www.cnblogs.com/qiusuo/p/5152406.html