【cmu15445c++入门】(4)c++中的模板方法

一、template模板方法

模板方法是c++的一个特性,可以让你的代码在不指定数据类型的情况下,运行不同的数据类型。

你可以创建模板方法和模板类,本文讨论模板方法。

二、代码

cpp 复制代码
// Includes std::cout (printing) for demo purposes.
#include <iostream>

// Templates are a language feature in C++ that allow you to write code that
// can work with multiple data types, without actually specifying those types.
// In C++, you can create both templated functions and templated classes. We'll
// talk about templated functions in this file.

//模板方法是c++的一个特性,可以让你的代码在不指定数据类型的情况下,运行不同的数据类型。
//你可以创建模板方法和模板类,这个文件讨论模板方法。

// Here is a basic templated function that adds two numbers.
// Syntax note: You will see code with both template<class T> and
// template<typename T>. Although these statements are equivalent, there are
// differences between the class and typename keywords. This blog article covers
// this difference, but you won't need to know this for the class:
// https://mariusbancila.ro/blog/2021/03/15/typename-or-class/

//模板方法,计算两个数字的和
template <typename T> 
T add(T a, T b) { return a + b; }

// It is possible to pass multiple type names via templates into functions.
// This function will print both of these values out.
// 传入多个类型的模板方法
template<typename T, typename U>
void print_two_values(T a, U b) {
  std::cout << a << " and " << b << std::endl;
}
template<typename T, typename U, typename V>
void print_three_values(T a, U b,V c) {
  std::cout << a << " and " << b << " and " << c <<std::endl;
}
template<typename T, typename U, typename V,typename W>
void print_four_values(T a, U b,V c,W d) {
  std::cout << a << " and " << b << " and " << d <<std::endl;
}
template<typename T, typename U, typename V,typename W,typename X>
void print_five_values(T a, U b,V c,W d,X e) {
  std::cout << a << " and " << b << " and " << e <<std::endl;
}

// It is also possible to create specialized templated functions, that do
// different things for different types. Take the following contrived example,
// which prints the type if its a float type, but just prints hello world for
// all other types.
// 也能根据不通的类型,制定不同的模板方法。
template <typename T> 
void print_msg() { std::cout << "Hello world!\n"; }

// Specialized templated function, specialized on the float type.
// 对float类型单独定制
template <> 
void print_msg<float>() {
  std::cout << "print_msg called with float type!\n";
}

// Lastly, template parameters do not have to be classes. Take this basic (yet
// very contrived) function that takes in a bool as a template parameter and
// does different things to the argument depending on the boolean argument.
// 这个做法不推荐
template <bool T> int add3(int a) {
  if (T) {
    return a + 3;
  }

  return a;
}

int main() {
  // First, let's see the add function called on both ints and floats.
  std::cout << "Printing add<int>(3, 5): " << add<int>(3, 5) << std::endl;
  std::cout << "Printing add<float>(2.8, 3.7): " << add<float>(2.8, 3.7)
            << std::endl;

  // It is also possible for a templated function to interpret the type of its
  // arguments, although if you're a beginner in modern C++, it's preferred you
  // don't do this because then you might not be sure of the types being passed
  // into your functions.
  // 也可以不指定类型,但初学者不建议
  std::cout << "Printing add(3, 5): " << add(3, 5) << std::endl;

  // Second, let's see the print_two_values function being called with two
  // different types.
  std::cout << "Printing print_two_values<int, float>(3, 3.2): ";
  print_two_values<int, float>(3, 3.2);

  print_three_values<int, float, double>(3, 3.2,2.14);
  print_four_values<int, float, double,float>(3, 3.2,2.14,2.12);
  print_five_values<int, float, double,float,int>(3, 3.2,2.14,2.12,6);
  // Let's see what happens when we called print_msg with and without the float
  // type being passed in. As expected, the first call to print_msg prints out
  // the general output, while the second one, with the float argument,
  // recognizes its type parameter and calls the specialized function.
  std::cout << "Calling print_msg<int>(): ";
  print_msg<int>();
  std::cout << "Calling print_msg<float>(): ";
  print_msg<float>();

  // add3 has the specified behavior for both a true and false templated
  // argument, as we can see here.
  std::cout << "Printing add3<true>(3): " << add3<true>(3) << std::endl;
  std::cout << "Printing add3<false>(3): " << add3<false>(3) << std::endl;

  // Lastly, it's important to note that most of these are contrived examples,
  // and it is possible to code some of these functions (e.g. passing a boolean
  // as an argument instead of a templated argument) without using templates.
  // However, in the class, you'll be seeing code similar to this in the
  // codebase, so it's good to understand templated functions in these contexts!
  // 最后,需要注意的是,以上大多数都是人为的示例,并且有的是可以不使用模板,而是对其中一些函数进行编码
  //(例如,将布尔值作为参数而不是模板化参数传递)。但是,海量的代码库中很可能也看到与此类似的代码,因此最好在这些上下文中理解模板化函数!
  return 0;
}

运行结果

相关推荐
草莓熊Lotso3 分钟前
【Linux 线程进阶】进程 vs 线程资源划分 + 线程控制全详解
java·linux·运维·服务器·数据库·c++·mysql
唐樽8 分钟前
C++ 竞赛学习路线笔记
c++·笔记·学习
ShineWinsu8 分钟前
对于Linux:文件操作以及文件IO的解析
linux·c++·面试·笔试·io·shell·文件操作
十五年专注C++开发33 分钟前
Oat++: 一个轻量级、高性能、零依赖的 C++ Web 框架
开发语言·c++·web服务·oatpp
John_ToDebug1 小时前
惰性绑定 vs 立即注入:Chromium 扩展 API 初始化策略深度对比
c++·chrome·v8
快乐的划水a2 小时前
c++计时器类
c++
山上三树2 小时前
预处理、编译、汇编、链接详解
c++
2301_789015622 小时前
C++:异常
开发语言·c++·异常·异常的处理方式
CVer儿2 小时前
c++接口内部内存分配问题设计
开发语言·c++
2301_789015622 小时前
C++:智能指针
c语言·开发语言·汇编·c++·智能指针