现代 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


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

相关推荐
Wang's Blog4 分钟前
Go-Zero项目开发33: IM实时通信前后端对接与微服务基础设施实践
开发语言·微服务·golang·go-zero
dyxal6 分钟前
eˣ 的泰勒展开式:从“化繁为简”到“万物统一”的微积分之美
算法
SelectDB技术团队13 分钟前
丰巢日志平台 ELK 替代:Apache Doris / SelectDB 的技术能力与实践
开发语言·python
BetterFlow_CFD14 分钟前
COMSOL声学仿真基础知识(二)
开发语言·算法·matlab
麻瓜老宋17 分钟前
AI开发C语言应用按步走,表达式计算器calc的第十九步,注释、表达式分隔符、long double 精度
c语言·开发语言·atomcode
崖边看雾18 分钟前
Python学习——函数
开发语言·windows·python·学习·pycharm
ShineWinsu24 分钟前
对于Linux:http的解析
linux·网络·c++·网络协议·http·请求·响应
宸津-代码粉碎机32 分钟前
Jar热部署进阶实战|修复原生方案OOM与类冲突问题,生产级无BUG优化方案
java·大数据·服务器·开发语言·前端·人工智能·python
choumin10 小时前
创建型模式——工厂方法模式
c++·设计模式·工厂方法模式·创建型模式
云小逸10 小时前
【SVN 详细使用指南:从入门到团队协作】
c++·svn