Skip to content

basic

template

function

class

typename Container = std::vector 即代表这个作为默认实现,

#include <iostream>
#include <string>
#include <vector>

// 定义模板类,带有默认模板参数
template <typename T, typename Container = std::vector<T>>
class MyTemplate {
public:
    void add(const T& value) {
        container.push_back(value);
    }

    void print() const {
        for (const auto& item : container) {
            std::cout << item << " ";
        }
        std::cout << std::endl;
    }

private:
    Container container;
};

int main() {
    // 使用默认的 Container 参数 std::vector<int>
    MyTemplate<int> intTemplate;
    //MyTemplate<int ,std::vector<int>> intTemplate; 
    //使用默认的实现, 完全就等于这样
    //如果有自定义的实现my_vector, 甚至可以 MyTemplate<int ,my_vector<int>> intTemplate; 

    intTemplate.add(1);
    intTemplate.add(2);
    intTemplate.add(3);
    intTemplate.print(); // 输出: 1 2 3

    // 显式提供 Container 参数 std::vector<std::string>
    MyTemplate<std::string, std::vector<std::string>> stringTemplate;
    stringTemplate.add("Hello");
    stringTemplate.add("World");
    stringTemplate.print(); // 输出: Hello World

    return 0;
}

class function member [c++ 14]

type aliases

variables

为相同变量提供不同的类型

#include <iostream>

// 模板变量 pi
template <class T>
inline constexpr T pi = T(3.1415926535L);

// 模板函数 circular_area
template <class T>
T circular_area(T r) {
    return pi<T> * r * r;
}

int main() {
    // 使用 int 类型
    int radius_int = 2;
    std::cout << "Area of circle with radius " << radius_int << " (int): " << circular_area(radius_int) << std::endl;

    // 使用 double 类型
    double radius_double = 2.0;
    std::cout << "Area of circle with radius " << radius_double << " (double): " << circular_area(radius_double) << std::endl;

    return 0;
}
解糖后
#include <iostream>

template<class T>
inline constexpr const T pi = T(3.14159265349999999991L);

template<>
inline constexpr const int pi<int> = int(3.14159265349999999991L);
template<>
inline constexpr const double pi<double> = double(3.14159265349999999991L);

template<class T>
T circular_area(T r)
{
  return (pi<T> * r) * r;
}

/* First instantiated from: insights.cpp:16 */
#ifdef INSIGHTS_USE_TEMPLATE
template<>
int circular_area<int>(int r)
{
  return (pi<int> * r) * r;
}
#endif


/* First instantiated from: insights.cpp:20 */
#ifdef INSIGHTS_USE_TEMPLATE
template<>
double circular_area<double>(double r)
{
  return (pi<double> * r) * r;
}
#endif


int main()
{
  int radius_int = 2;
  std::operator<<(std::operator<<(std::cout, "Area of circle with radius ").operator<<(radius_int), " (int): ").operator<<(circular_area(radius_int)).operator<<(std::endl);
  double radius_double = 2.0;
  std::operator<<(std::operator<<(std::cout, "Area of circle with radius ").operator<<(radius_double), " (double): ").operator<<(circular_area(radius_double)).operator<<(std::endl);
  return 0;
}

static datamember

根据传入参数的类型不同为静态变量设置不同的结果, 最终可以通过该静态变量确认传参的【类型、其他个性化操作 】

#include <iostream>

// 自定义类型特征
template <typename T>
struct is_pointer {
    static constexpr bool value = false;  // 默认情况下,T 不是指针类型
};

// 特化版本,用于检查指针类型
template <typename T>
struct is_pointer<T*> {
    static constexpr bool value = true;  // T* 是指针类型
};

// 变量模板
template <typename T>
inline constexpr bool is_pointer_v = is_pointer<T>::value;

int main() {
    std::cout << std::boolalpha;  // 输出布尔值为 true 和 false

    std::cout << "Is int* pointer? " << is_pointer_v<int*> << std::endl;  // 输出: true
    std::cout << "Is int pointer? " << is_pointer_v<int> << std::endl;  // 输出: false
    std::cout << "Is double* pointer? " << is_pointer_v<double*> << std::endl;  // 输出: true
    std::cout << "Is std::string* pointer? " << is_pointer_v<std::string*> << std::endl;  // 输出: true

    return 0;
}

lambda