c++杂谈-3


一、完美转发

在函数模板中,可以将参数“完美”地转发给其它函数。所谓完美,即不仅能准确的转发参数的值,还能保证被转发参数的左、右值属性不变。C++11标准引入了右值引用和移动语义,所以,能否实现完美转发,决定了该参数在传递过程使用的是拷贝语义还是移动语义。

为了支持完美转发,C++11提供了以下方案:

  1. 如果模板中(包括类模板和函数模板)函数的参数书写成为T&&,那么函数既可以接受左值引用,也可以接受右值引用——注意不是具体类型,比如int&&,它是右值引用。
  2. 提供了模板函数std:forward<T>(参数),用于转发参数,如果参数是一个右值,转发之后仍是右值引用;如果参数是一个左值,转发之后仍是左值引用。

最初版本

#include <iostream>
using namespace std;

void func1(int &lvalue) { // 左值版本
	cout << "parameter is lvalue: " << lvalue << endl;
}

void func1(int &&rvalue) { // 右值版本
	cout << "parameter is rvalue: " << rvalue << endl;
}

template<typename T>
void func2(T &param) { // 左值版本
	func1(param);
}

template<typename T>
void func2(T &&param) { // 右值版本
	func1(move(param));
}

int main(int argc, const char **argv) {

	int a = 3;
//	func1(a); 实参是左值 parameter is lvalue: 3
//	func1(8); 实参是右值 parameter is rvalue: 8
	func2(a); // parameter is lvalue: 3
	func2(8); // parameter is lvalue: 8

	return 0;
}

引入完美转发后的版本

#include <iostream>
using namespace std;

void func1(int &lvalue) { // 左值版本
	cout << "parameter is lvalue: " << lvalue << endl;
}

void func1(int &&rvalue) { // 右值版本
	cout << "parameter is rvalue: " << rvalue << endl;
}

template<typename T>
void func(T &&param) { // 既可以接收左值引用,也可以接收右值引用
	func1(forward<T>(param));
}

int main(int argc, const char **argv) {

	int a = 3;
	func(a);
	func(8);

	return 0;
}

二、c++可变参数

  1. 可变参数的基本用法
#include <iostream>
using namespace std;

template<typename T>
void show(T t) {
	cout << "using: " << t << endl;
}

// 递归终止时调用的非模版函数,函数名要与展开参数包的递归函数模版的函数名相同。
void print() { // 递归结束时调用的是无参函数——此时参数已经没有了
	cout << "finish" << endl;
}

template<typename T, typename ...Args>
void print(T arg, Args ... args) { // 可变参数支持
	cout << "Arguments: " << arg << endl; // 显示本次展开的参数
	show(arg);
	cout << "There are " << sizeof...(args) << " args left" << endl;
	print(args...); // 递归,继续展开剩下的参数,每调用一次,args中的参数就少一个。
}

int main(int argc, const char **argv) {

	print("abc", 9, 10, "kkk", "ldd");

	return 0;
}
  1. 常规参数+可变参数
#include <iostream>
using namespace std;

void print() {
	cout << "finish" << endl;
}

template<typename T, typename ...Args>
void print(T arg, Args ... args) {
	cout << "Arguments: " << arg << endl;
	print(args...);
}

template<typename ...Args>
void func(const string &str, Args ...args) { // 第一个参数是常规参数,之后是可变参数
	cout << str << endl;
	print(args...);
}

int main(int argc, const char **argv) {

	func("let's go", 9, 10, "hi", "girl");

	return 0;
}

三、时间操作与chrono库

C++11提供了chrono模版库,实现了一系列时间相关的操作(时间长度、系统时间和计时器)。
头文件:#include <chrono>
名字空间:std::chrono

using hours = duration<Rep,std::ratio<3600>> // 小时
using minutes = duration<Rep,std::ratio<60>> // 分钟
using seconds = duration<Rep> // 秒
using milliseconds = duration<Rep,std::milli> // 毫秒
using microseconds = duration<Rep,std::micro> // 微秒
using nanoseconds = duration<Rep, std::nano> // 纳秒
  1. 基本时间的操作
#include <iostream>
#include <chrono>
using namespace std;

int main(int argc, const char **argv) {

	chrono::hours t1(1); // 1小时
	chrono::minutes t2(60); // 60分钟
	chrono::seconds t3(60 * 60); // 3600秒,即1小时
	chrono::milliseconds t4(60 * 60 * 1000); // 60*60*1000毫秒,即1小时

	if (t1 == t2)
		cout << "t1=t2" << endl;
	if (t1 == t3)
		cout << "t1=t3" << endl;
	if (t1 == t4)
		cout << "t1=t4" << endl;

	// 获取时钟周期的值,返回的是int整数
	cout << "t1=" << t1.count() << endl;
	cout << "t2=" << t2.count() << endl;
	cout << "t3=" << t3.count() << endl;
	cout << "t4=" << t4.count() << endl;

	return 0;
}
  1. 系统时间
#define _CRT_SECURE_NO_WARNINGS //localtime()需要这个宏
#include <iostream>
#include <chrono>
#include <iomanip> // put_time()函数
#include <sstream>
using namespace std;

int main(int argc, const char **argv) {

	// 静态成员函数chrono::system_clock::now()用于获取系统时间。(C++时间)
//	chrono::time_point < chrono::system_clock > now = chrono::system_clock::now();
	// 下边赋值的类型可以用auto来简化
	auto now = chrono::system_clock::now();

	// 静态成员函数chrono::system_clock::to_time_t()把系统时间转换为time_t。(UTC时间)
	time_t t_now = chrono::system_clock::to_time_t(now);

	// 把当前时间加1天
//	t_now = t_now + 24 * 60 * 60;
	// 把当前时间减小1小时
//	t_now = t_now + -1 * 60 * 60;
	// 把当前时间加120秒
//	t_now = t_now + 120;

	// std::localtime()把time_t转换成本地时间。(北京时间)
	// localtime()不是线程安全的,Windows用localtime_s代替,Linux用localtime_r代替。
	tm *tm_now = localtime(&t_now);

	// 格式化输出本地时间
	cout << put_time(tm_now, "%Y-%m-%d %H:%M:%S") << endl;
	cout << put_time(tm_now, "%Y-%m-%d") << endl;
	cout << put_time(tm_now, "%H:%M:%S") << endl;
	cout << put_time(tm_now, "%Y%m%d%H%M%S") << endl;

	stringstream ss;
	ss << put_time(tm_now, "%Y-%m-%d %H:%M:%S");
	string time_str = ss.str();
	cout << time_str << endl;

	return 0;
}
  1. 计时器
#include <iostream>
#include <chrono>
using namespace std;

int main(int argc, const char **argv) {

	// 获取开始时间点
	chrono::steady_clock::time_point start = chrono::steady_clock::now();

	//一些工作
	cout << "begin" << endl;
	for (int i = 0; i < 1000 * 1000; ++i) {
		// do somethings
	}
	cout << "time up" << endl;

	chrono::steady_clock::time_point end = chrono::steady_clock::now();

	auto dt = end - start;
	cout << "It consumes " << dt.count() << " nanoseconds ("
			<< (double) dt.count() / (1000 * 1000 * 1000) << " seconds)"
			<< endl;

	return 0;
}

四、避免异常的方法

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;

int main(int argc, const char **argv) {
	double *ptr = nullptr;
	ptr = new (std::nothrow) double[10000000000000];
	if (ptr == nullptr)
		cout << "malloc failed" << endl;
	if (ptr != nullptr)
		delete[] ptr;

	return 0;
}

以下代码演示把基类引用转换为派生类引用时发生的异常情况

HX hx;
Hero& rh=hx;
try{
	XS& rxs=dynamic_cast<XS&>(rh);
}catch(bad_cast){
	cout << "失败的类型转换" << end;
}

避免的方法

if(typeid(XS)==typeid(rh)){
	XS& rxs=dynamic_cast<XS&>(rh);
else
	cout << "类型不匹配,无法转换" << endl;
}

五、Eclipse CDT 的CMake配置文件

学院派的IDE,但为了增加通用性和主流性,项目管理使用CMake。
配置文件CMakeLists.txt的例子:

cmake_minimum_required(VERSION 3.15)
project(testbed)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

#add_definitions("-Wall -g -fexec-charset=gbk")
add_definitions("-Wall -g")

aux_source_directory("${CMAKE_CURRENT_SOURCE_DIR}/src" SRC)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/include ${CMAKE_CURRENT_SOURCE_DIR}/../../boost_1_81_0)

#find_package(SQLiteCpp REQUIRED)

add_executable(testbed ${SRC})
#target_link_libraries(testbed PRIVATE SQLiteCpp)

这里Eclipse的编译器使用Windows下的MinGW64。

注意:.c,.cc,.cpp才是编译单元,会被编译;但.h头文件不会被编译。.h主要用于编译时对包含头文件的编译单元内的类,函数等进行语法比对。

对Eclipse CDT的必要配置(针对CMake):
Windows -> Preferences-> C/C++ -> CMake -> Add…

六、c++断言

断言(assertion)是一种常用的编程手段,用于排除程序中不应该出现的逻辑错误。使用断言需要包含头文件<cassert>或<assert.h>,头文件中提供了带参数的宏assert,用于程序在运行时进行断言。
语法:assert(表达式)
断言就是判断(表达式)的值,如果为0(false),程序将调用abort()函数中止,如果为非0(true),程序继续执行。

#include <iostream>
#include <cassert>
using namespace std;

void copydata(void *p1, void *p2) {
	assert(p1 && p2);
	cout << "continue" << endl;
}

int main(int argc, const char **argv) {
	int i = 0, j = 0;
	copydata(nullptr, &j);

	return 0;
}

断言不是异常,是不能用try块捕获的。
assert宏是运行时断言,在程序运行的时候才能起作用。C++11新增了静态断言static_assert,用于在编译时检查源代码。使用静态断言不需要包含头文件。
语法:static_assert(常量表达式,提示信息)
注意:static_assert的第一个参数是常量表达式,而assert的表达式既可以是常量,也可以是变量。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值