美文网首页
c++ 11 特性

c++ 11 特性

作者: HenryTien | 来源:发表于2017-04-16 07:39 被阅读10次
  1. lambda特性
  2. 括号初始化列表 std::initializer_list
  3. 代理构造函数
  4. 字符串字面增量
  5. 用户定义 字面量
#include <iostream>

long double operator"" _mm(long double x) { return x / 1000; }
long double operator"" _m(long double x)  { return x; }
long double operator"" _km(long double x) { return x * 1000; }

int main()
{
    std::cout << 1.0_mm << '\n';
    std::cout << 1.0_m  << '\n';
    std::cout << 1.0_km << '\n';
}
  1. 右值引用和move语义
  • 为了更加高效的使用临时变量
MyString(MyString && str){
    std::cout<<"Move Constructor is called: source: "<<str._data<<std::endl;
    _len=str._len;
    _data=str._data;
    str._len=0;
    str._data=NULL;
}
  1. 强类型枚举
  1. 可变参数模板和打包

#include<iostream>
using namespace std;
template<class ... T>
void f(T ... args){
    cout<<sizeof... (args)<<endl; // 打印可变参数的个数
}

int main(){
    f();
    f(1,3);
    f(2,3.5,"");
}

Parameter pack

  1. constexpr specifier
    evaluate the value of the function or variable at compile time.

A constexpr variable must satisfy the following requirements:
its type must be a LiteralType
.
it must be immediately initialized
the full-expression of its initialization, including all implicit conversions, constructors calls, etc, must be a constant expression

A constexpr function must satisfy the following requirements:
it must not be virtual
its return type must be LiteralType

each of its parameters must be LiteralType

there exists at least one set of argument values such that an invocation of the function could be an evaluated subexpression of a core constant expression (for constructors, use in a constant initializer is sufficient) (since C++14). No diagnostic is required for a violation of this bullet.

相关文章

  • C++ 11常用特性

    C++ 11常用特性(转)

  • C++ 11 语言特性介绍

    C++ 11 语言特性 新特性说明nullptr,autoNAfor-each区间迭代shared_ptr uni...

  • c++ 11 特性

    lambda特性 括号初始化列表 std::initializer_list 代理构造函数 字符串字面增量 用户定...

  • coco2d-x v3.0 亮点

    v3.0 亮点 @来自官网 使用 C++(C++11) 的特性取代了 Objective-C 的特性优化了 Lab...

  • 技能

    C++ C++特性 C++11 多态和继承 构造函数 析构函数 手写代码实现string类 手写代码实现智能指针 ...

  • vscode支持C++11

    一. 背景:C++部分特性需要C++11支持,因此需要设置支持C++11。例如:vector list = {1...

  • 2018-10-23 step

    C++ 11 几个特性的整理 std::future - std::promise 解决的问题 返回值的异步获取:...

  • cppinsights 编译安装

    cppinsights 是一款C++源代码到源代码的转换,它可以把C++中的模板、auto以及C++11新特性展开...

  • C++ 11的一些新特性(1)

    最近学习了一下C++11的新特性,发现c++的进步很快,已经不在是我最早认识的那个c++了。 我的感觉是c++的更...

  • C++ 11的一些新特性(2)

    最近学习了一下C++11的新特性,发现c++的进步很快,已经不在是我最早认识的那个c++了。 我的感觉是c++的更...

网友评论

      本文标题:c++ 11 特性

      本文链接:https://www.haomeiwen.com/subject/ymmhattx.html