C11
1.区别于C语言的耍流氓强转转换
C语言强转
特点:C语言中所有的转换都是一个(),但是并不知道这样做的目的是什么
C++强转
static_cast<>:数据类型的转换
特点:只是值的转换
const_cast<>:去除常性的强转
reinterpret_cast<>:重新解释
dynamic_cast<>:动态转换
动态转换存在于继承关系中
继承关系的产生一定是因为有虚函数;没有虚函数就不要有继承关系。继承关系带来的问题就是因为有虚函数,产生了多态
2.std::move的底层实现
思路:
1.有一个泛化版本,一个普通引用的部分特化版本,一个右值引用的部分特化版本;
2.都使用 type 得到其原始数据类型;
3.获取原始类型的 type
4.进行类型的转换,强转为原生类型的右值引用类型
5.并没有把_Ty中的常性去除,这就导致如果参数是一个常引用,调用移动构造函数时系统会直接调用拷贝构造函数
template<class _Ty>
struct my_remove_reference
{
using type = _Ty;
using _Const_thru_ref_type = const _Ty;
};
template<class _Ty>
struct my_remove_reference<_Ty&>
{
using type = _Ty;
using _Const_thru_ref_type = const _Ty&;
};
template<class _Ty>
struct my_remove_reference<_Ty&&>
{
using type = _Ty;
using _Const_thru_ref_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);
}
3.完美转发
模板中的&&意思是类型未定义
std::forward<>的底层实现
template<class _Ty>
struct my_remove_reference
{
using type = _Ty;
using _Const_thru_ref_type = const _Ty;
};
template<class _Ty>
struct my_remove_reference<_Ty&>
{
using type = _Ty;
using _Const_thru_ref_type = const _Ty&;
};
template<class _Ty>
struct my_remove_reference<_Ty&&>
{
using type = _Ty;
using _Const_thru_ref_type = const _Ty&&;
};
template<class _Ty>
using my_remove_reference_t = typename my_remove_reference < _Ty>::type;
template<class _Ty>
_Ty&& my_forward(my_remove_reference_t < _Ty>& Arg)
{
return static_cast<_Ty&&>(Arg);
}
4.auto
1.auto实际是占位符,编译过程中推导出类型后就会将auto替换
2.auto定义的变量必须初始化,因为编译器要通过值来推导变量的类型
3.不能给出二义性初始值
4.auto不能作为函数参数
5.auto不能成为集合类型
5.decltype:根据表达式推演出类型
6.NULL和nullptr的区别
无论是定义为0,还是无类型,编译器都把它当作整型对待。因此C11为了解决该问题,发明了一个关键字 “ nullptr” ,认为它是一个空指针常量
7.列表初始化
对于所有的内置类型或自定义类型的初始化方案都可以使用列表方式
动态开辟空间