C++之template可变模板参数应用总结(二百二十八)

发布于:2023-09-22 ⋅ 阅读:(161) ⋅ 点赞:(0)

简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!

优质专栏:Audio工程师进阶系列原创干货持续更新中……】🚀

人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.

更多原创,欢迎关注:Android系统攻城狮

欢迎关注Android系统攻城狮

1.前言

本篇目的:C++之template可变模板参数应用总结。

1.在C++中,template<typename ... Deps>中的...是一个可变模板参数(variadic template arguments)语法,用于表示可接受零个或多个类型参数的模板。

2.可变模板参数允许我们在使用模板时传递不确定数量的参数,将其展开为模板的多个实参。这样,我们可以在使用模板时灵活地指定任意数量的类型参数,而不限制于固定数量的参数。

2.应用实例

v1.0 打印任意数量的参数

#include <iostream>

template<typename ... Args>
void printValues(Args ... args) {
    ((std::cout << args << " "), ...);
}

int main() {
    printValues(1, "hello", 3.14);
    // 输出:1 hello 3.14
    return 0;
}

v2.0 计算可变参数的和

#include <iostream>

template<typename ... Args>
auto sumValues(Args ... args) {
    return (args + ...);
}

int main() {
    int sum = sumValues(1, 2, 3, 4, 5);
    // 输出:15
    return 0;
}

v3.0 构造tuple包含不同类型的对象

#include <iostream>
#include <tuple>

template<typename ... Args>
auto createTuple(Args ... args) {
    return std::make_tuple(args...);
}

int main() {
    auto myTuple = createTuple(1, "hello", 3.14);
    // myTuple 的类型为 std::tuple<int, const char*, double>
    return 0;
}

v4.0 展开参数并应用函数

#include <iostream>
#include <functional>

template<typename ... Args>
void applyFunction(std::function<void(Args...)> fn, Args ... args) {
    fn(args...);
}

void printValues(int a, const std::string& b, double c) {
    std::cout << a << " " << b << " " << c << std::endl;
}

int main() {
    applyFunction(printValues, 1, "hello", 3.14);
    // 输出:1 hello 3.14
    return 0;
}

v5.0 递归展开参数包

#include <iostream>

template<typename T>
void processValue(T value) {
    std::cout << value << " ";
}

template<typename T, typename ... Rest>
void processValue(T value, Rest ... rest) {
    processValue(value);
    processValue(rest...);
}

int main() {
    processValue(1, "hello", 3.14);
    // 输出:1 hello 3.14
    return 0;
}

v6.0 使用可变参数模板作为函数重载

#include <iostream>
#include <string>

template<typename T>
void printValue(T value) {
    std::cout << value << std::endl;
}

template<typename T, typename ... Args>
void printValue(T value, Args ... args) {
    std::cout << value << " ";
    printValue(args...);
}

int main() {
    printValue(1, "hello", 3.14);
    // 输出:
    // 1
    // hello
    // 3.14
    return 0;
}

v7.0 模板递归展开参数包

#include <iostream>

template<typename ... Args>
void printValues(Args ... args) {
    ((std::cout << args << " "), ...);
}

int main() {
    std::tuple<int, std::string, double> myTuple(42, "hello", 3.14);
    std::apply(printValues<Args...>, myTuple);
    // 输出:42 hello 3.14
    return 0;
}

v8.0 使用可变参数模板定义类

#include <iostream>

template<typename ... Args>
class MyClass {
public:
    void printValues(Args ... args) {
        ((std::cout << args << " "), ...);
    }
};

int main() {
    MyClass<int, std::string, double> instance;
    instance.printValues(42, "hello", 3.14);
    // 输出:42 hello 3.14
    return 0;
}

网站公告

今日签到

点亮在社区的每一天
去签到