现代 C++ 使用教程

std::ref

template<typename T>

auto print_type_info(const T& t) {

if constexpr (std::is_integral<T>::value) {

return t + 1;

} else {

return t + 0.001;

}

}

template<typename T = int, typename U = int>

auto add(T x, U y) -> decltype(x+y) {

return x+y;

}

// sizeof... 不定参数个数

template<typename... Ts>

void magic(Ts... args) {

std::cout << sizeof...(args) << std::endl;

}

// 1. recursive parameter unpack

template<typename T0>

void printf1(T0 value) {

std::cout << value << std::endl;

}

template<typename T, typename... Ts>

void printf1(T value, Ts... args) {

std::cout << value << std::endl;

printf1(args...);

}

template<typename T, typename... Ts>

auto printf3(T value, Ts... args) {

std::cout << value << std::endl;

(void) std::initializer_list<T>{([&args] {

std::cout << args << std::endl;

}(), value)...};

}

template<typename ... T>

auto sum(T ... t) {

return (t + ...);

}

static std::mutex mtx;

std::lock_guard<std::mutex> lock(mtx);

// pack a lambda expression that returns 7 into a std::packaged_task

std::packaged_task<int()> task([](){return 7;});

// get the future of task

std::future<int> result = task.get_future(); // run task in a thread

std::thread(std::move(task)).detach();

std::cout << "waiting...";

result.wait(); // block until future has arrived

// 限制参数是否满足指定用法

template<typename T>

concept bool Stringable = requires(T a){

{a.to_string()} -> string;

};

void print(Stringable a){

std::cout << a.to_string() << std::endl;

}

struct Person {

double height, weight;

Person(double a, double b) : height(a), weight(b) {}

string to_string(){

return "weight: "+ std::to_string(weight) + ", height: "+ std::to_string(height);

}

};

Person p(57, 170.0);

print(p); // uses concept Stringable


创作不易,小小的支持一下吧!

相关推荐
中國龍在廣州2 分钟前
“物理AI”吹响号角
大数据·人工智能·深度学习·算法·机器人·机器人学习
小六子成长记3 分钟前
【C++】:多态的实现
开发语言·c++
flysh054 分钟前
关于C#编程中的async / await关键字
开发语言·c#
好好学操作系统4 分钟前
flash_attn ImportError undefined symbol:
开发语言·python
CCPC不拿奖不改名5 分钟前
面向对象编程:继承与多态+面试习题
开发语言·数据结构·python·学习·面试·职场和发展
chen_2275 分钟前
动态桌面方案
c++·qt·ffmpeg·kanzi
liulilittle6 分钟前
OPENPPP2 Code Analysis Three
网络·c++·网络协议·信息与通信·通信
꧁Q༒ོγ꧂6 分钟前
算法详解(二)--算法思想基础
java·数据结构·算法
꧁Q༒ོγ꧂7 分钟前
算法详解(一)--算法系列开篇:什么是算法?
开发语言·c++·算法
橘颂TA7 分钟前
【剑斩OFFER】算法的暴力美学——力扣:1047 题:删除字符串中的所有相邻重复项
c++·算法·leetcode·职场和发展·结构于算法