声明类和函数时在 C++ 中使用模板 <class T>

cer*_*rou -4 c++ templates

我对 C++ 中的模板主题很陌生。为什么在下面的玩具示例代码中,我们必须在类和每个函数的名称之前加上template <class T>(这意味着我们为什么需要它)?

是否可以修改代码不template <class T>随处使用?

#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept>

using namespace std;

template <class T>
class Stack {   private:
    vector<T> elements;

  public:
    void push(T const &);
    void pop();
    T top();
    bool empty(); };

template <class T>
void Stack<T>::push(T const &elem) {
    elements.push_back(elem); }

template <class T>
void Stack<T>::pop() {
    if (elements.empty()) {
        throw out_of_range("Stack<>::pop(): empty stack");
    } else {
        elements.pop_back();
    }
}

template <class T>
T Stack<T>::top() {
    if (empty()) {
        throw out_of_range("Stack<>::top(): empty stack");
    }
    return elements.back();
}

template <class T>
bool Stack<T>::empty() {
    return elements.empty();
}


int main() {
    try {
        Stack<int> intStack;       // Stack of ints
        Stack<string> stringStack; // Stack of strings

        // Manipulate integer stack
        intStack.push(7);
        cout << intStack.top() << endl;

        // Manipulate string stack
        stringStack.push("hello");
        cout << stringStack.top() << std::endl;
        stringStack.pop();
        stringStack.pop();
    }
    catch (exception const &ex) {
        cerr << "Exception: " << ex.what() << endl;
        return -1;
    }
}
Run Code Online (Sandbox Code Playgroud)

txt*_*elp 12

C++ 是一种静态类型语言。

既然是这种情况,假设我想要一个函数来检查两个值的最小值或最大值。

我可以创建一个如下所示的函数:

int max(int a, int b) { return (a > b) ? a : b; }
Run Code Online (Sandbox Code Playgroud)

但这仅适用于int类型。如果我想要一个用于 a doubleor uint64_tor的函数FooBarClass,我需要创建其他函数:

double max(double a, double b) { return (a > b) ? a : b; }
uint64_t max(uint64_t a, uint64_t b) { return (a > b) ? a : b; }
FooBarClass max(FooBarClass a, FooBarClass b) { return (a > b) ? a : b; }
Run Code Online (Sandbox Code Playgroud)

为什么在下面的玩具示例代码中,我们必须在类和每个函数的名称前面加上 template ?

在 C++ 中, atemplate是引入“泛型”概念的一种方式。使用泛型,您不再需要关心为每种类型创建函数,类型将从模板函数签名中推导出来。

因此,我可以写以下内容:

template < typename T > // the typename or class keywords are synonymous when declaring a template
T max(T a, T b) { return (a > b) ? a : b; }
Run Code Online (Sandbox Code Playgroud)

函数签名的包含template < typename T >向编译器表明该函数是一个“通用”函数,必须进行相应的处理。

另一方面,如果您只有以下情况:

T max(T a, T b) { return (a > b) ? a : b; }
Run Code Online (Sandbox Code Playgroud)

编译器将期望T是一个显式类型(如typedef int T;)。

模板可以同时应用于类或函数:

template < typename T >
class Wrapper {
    public:
        Wrapper(const T& val) : _val(val) {}
        // ... other code

        template < typename X >
        operator X()
        {
            return static_cast<X>(this->_val);
        }

        T operator+(const T& val)
        {
            return this->_val + val;
        }

        friend std::ostream& operator<<(std::ostream& os, const Wrapper& val)
        {
            os << val._val;
            return os;
        }

    private:
        T _val;
};

int main(int argc, char* argv[])
{
    Wrapper<int> x(42);
    // template deduction will call Wrapper::operator X() with X being a double
    double d = x;
    // template deduction will call Wrapper::operator X() with X being a long
    long l = x;
    // this actually calls T operator+(const T& val) with val = 10 and T = int
    int i = x + 10;

    std::cout << "x = " << x << std::endl  // 42
              << "d = " << d << std::endl  // 42
              << "l = " << l << std::endl  // 42
              << "i = " << i << std::endl; // 52
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

您还可以显式定义模板类型,这称为模板专业化 ,考虑到您目前对模板的理解,这超出了本答案的范围。

是否可以修改代码而不是到处使用模板?

是和不是。是的,您可以删除template < class T >签名定义,但是,如上所述,类/函数的签名具有完全不同的含义。

希望事情能澄清一点。如果使用得当,模板是 C++ 中非常强大的工具。


Cal*_*eth 5

您必须包含template <class T>在每个成员定义中,因为这是成员名称的一部分。

您可以在类模板主体中定义函数,只需一行 template <class T>

template <class T> 
class Stack {   
  private:
    vector<T> elements;

  public:
    void push(T const &) {
      elements.push_back(elem); }
    void pop() {
      if (elements.empty()) {
          throw out_of_range("Stack<>::pop(): empty stack");
      } else {
          elements.pop_back();
      } }
    T top() {
      if (empty()) {
          throw out_of_range("Stack<>::top(): empty stack");
      }
      return elements.back(); }
    bool empty() {
      return elements.empty(); } };
Run Code Online (Sandbox Code Playgroud)