类型转换 完美转发 auto nullptr与NULL

C11的特性

类型转换
const int a=10;

int* p=const_cast<int*>(a);//去常性转换
p=static_cast<int*>(&a);//类型转换,但是不会成功,普通类型转换
char* cp=reinterpret_cast<char*>(&a);//指针类型转换


Object* op=NULL;
Base* bp=NULL;

Object obj;
Base base;

op=dynamic_cast<Object*>(&base>;//将父指针指向子对象

新的重命名规则

typedef int KeyType;
using KeyType=int; 
完美转发

右值在发生转发的过程中不发生改变
c11标准里能取地址的叫做左值,不能取地址的叫做右值,还有将亡值
c11里面的函数重载把右值也考虑在里面

void print(int a)
{
}
void print(int& a)
{
}
void print(int&& a)
{
}
template<class _Ty>
void fun(_Ty&& a)
{
	print(std::forward<_Ty>(a));//完美转发
}

模板类里面是一个右值引用的时候,可以引用左值,右值,常引用,

移动构造是怎么实现的,将对象当作参数传进去,将那个对象像类型萃取一样萃取出来,然后类型转换成右值,在调用这个类型的移动构造构造对象

#include<iostream>

using namespace std;

template<class _Ty>
struct my_remove_reference
{
	using type = _Ty;
	using _Const_thru_tef_type = const _Ty;
};

template<class _Ty>
struct my_remove_reference<_Ty&>
{
	using type = _Ty;
	using _Const_thru_tef_type = const _Ty&;
};

template<class _Ty>
struct my_remove_reference<_Ty&&>
{
	using type = _Ty;
	using _Const_thru_tef_type = const _Ty&&;
};

template<class _Ty>
using my_remove_reference_t = typename my_remove_reference<_Ty>::type;

template<class _Ty>
my_remove_reference_t<_Ty>&& my_move(_Ty&& Arg)
{
	return static_cast<my_remove_reference_t<_Ty>&&>(Arg);
}

template<class _Ty>
_Ty&& my_forward(my_remove_reference<_Ty>& Arg)
{
	return static_cast<_Ty&&>(Arg);
}
template<class _Ty>
_Ty&& my_forward(my_remove_reference<_Ty>&& Arg)
{
	return static_cast<_Ty&&>(Arg);
}
int main()
{
	
}
auto
int main()
{
	int x=1;
	auto* a=&x;//auto int,int *
	auto b=&x;//auto int*
	auto& c=x;//auto int
	const int e=10;
	auto f=e;//auto int;
	auto& f2=e;//atuo const int
}

decltype 类型推演

int add(int a,int b)
{
	return a+b;
}
int main()
{
	int a=10;
	decltype(a) b=20;
	decltype(add(12,23)) c=30;
}

nullptr与NULL

NULL类型匹配时候就等于0,匹配int类型
nullptr是一个关键字,指针类型的,空指针常量,类型匹配的时候匹配指针类型

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值