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


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

相关推荐
FRing031 小时前
vue2干货总结12(loading效果、全局前置导航守卫、搜索历史管理)
开发语言·前端·javascript·vue.js·vue2
wefg11 小时前
【项目】C++ 基于 Vibe Coding 的 OJ 平台
开发语言·c++
琳少_5 小时前
java程序设计笔记
java·开发语言·数据库·笔记
Helix2507 小时前
JDK高频报错速查手册:javac不是内部命令、内存溢出、版本不兼容等12类问题解决方法
java·开发语言·内存溢出·java异常处理·jdk版本兼容·jdk报错·javac报错
郝学胜-神的一滴8 小时前
Effective STL 条款 07:指针容器销毁前先删除
开发语言·c++·vscode·leetcode·rpc·visual studio
CoderYanger8 小时前
A.每日一题:2243. 计算字符串的数字和
java·开发语言·程序人生·leetcode·面试·蓝桥杯·学习方法
摇滚侠9 小时前
《On Java 中文版 基础卷》阅读笔记 接口 19
java·开发语言·笔记
吃我一窝jio10 小时前
Java多线程核心知识点梳理
java·开发语言·笔记
Lucas. Wáng10 小时前
Qt学习之旅
c++·qt