Non-static data member initializers

  1. 非静态成员变量初始化变得简单,这个XE7 64位编译成功,32位还是不支持

As a simple example,

struct TPerson
 {
  String aname = "张三";
 };

  1. class A {
  2. public:
  3. int a = 7;
    String aName = "MyName";
  4. };

would be equivalent to

  1. class A {
  2. public:
  3. A() : a(7) {}
  4. };

  5. http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2008/n2756.htm
    http://docwiki.embarcadero.com/RADStudio/XE7/en/Main_Page
    C++11 Features Supported by RAD Studio Clang-based C++ Compilers
    http://docwiki.embarcadero.com/RADStudio/XE7/en/C%2B%2B11_Features_Supported_by_RAD_Studio_Clang-based_C%2B%2B_Compilers
  6.  
  7. XE76411支持的全面,32很少。
    http://docwiki.embarcadero.com/RADStudio/XE7/en/C%2B%2B11_Language_Features_Compliance_Status
  8.  
  9. 11新特性
    http://clang.llvm.org/cxx_status.html
    http://cpprocks.com/9-reasons-to-start-using-c11/

Multi-declarator auto

The C++11 standard includes the multi-variable form of auto declarations, such as:

  1. int* func(){}
  2.  
  3. int _tmain(int argc, _TCHAR* argv[])
  4. {
  5. auto x = 3, * y = func(), z = 4;
  6. return 0;
  7. }
  1. 32位支持这样初始化
    class TNoteJSON
    {
    public:
     struct TNames
     {
      static String Title;
      static String Content;
      static String Id;
    }
    }
  2.  
  3. String TNoteJSON::TNames::Title = "title";
    String TNoteJSON::TNames::Content = "content";
    String TNoteJSON::TNames::Id = "id";
  4.  
  5. Caption = TNoteJSON::TNames::Title;
    TNoteJSON  tc;
    Caption = tc.at.Title;
    Caption = tc.at.Content;
  6.  
  7. C++0x Features Index
    http://docwiki.embarcadero.com/RADStudio/XE7/en/C%2B%2B0x_Features_Index
    Topics
    alignof Operator (C++0x)

其中__is_member_function_pointer__is_enum、__is_base_of很有用

http://docwiki.embarcadero.com/RADStudio/XE7/en/Strongly_Typed_Enums_(C%2B%2B0x)

http://docwiki.embarcadero.com/RADStudio/XE7/en/Type_Trait_Functions_(C%2B%2B0x)_Index

std::vector<int> numbers;

for(auto i : numbers)
   ListBox2->Items->Add(i);

http://towriting.com/blog/2013/08/01/what-is-cpp11/

http://developer.51cto.com/art/201312/422379.htm

http://www.cnblogs.com/hujian/archive/2012/12/10/2810813.html

http://cpprocks.com/c11-compiler-support-shootout-visual-studio-gcc-clang-intel/

什么是C++11

C++11是曾经被叫做C++0x,是对目前C++语言的扩展和修正,C++11不仅包含核心语言的新机能,而且扩展了C++的标准程序库(STL),并入了大部分的C++ Technical Report 1(TR1)程序库(数学的特殊函数除外)。

C++11包括大量的新特性:包括lambda表达式,类型推导关键字auto、decltype,和模板的大量改进。

本文将对C++11的以上新特性进行简单的讲解,以便大家能够快速了解到C++11对C++的易用性方面祈祷的巨大作用。

新的关键字

auto

C++11中引入auto第一种作用是为了自动类型推导

auto的自动类型推导,用于从初始化表达式中推断出变量的数据类型。通过auto的自动类型推导,可以大大简化我们的编程工作

auto实际上实在编译时对变量进行了类型推导,所以不会对程序的运行效率造成不良影响

另外,似乎auto并不会影响编译速度,因为编译时本来也要右侧推导然后判断与左侧是否匹配。

  1. auto a; // 错误,auto是通过初始化表达式进行类型推导,如果没有初始化表达式,就无法确定a的类型
  2. auto i = 1;
  3. auto d = 1.0;
  4. auto str = "Hello World";
  5. auto ch = 'A';
  6. auto func = less<int>();
  7. vector<int> iv;
  8. auto ite = iv.begin();
  9. auto p = new foo() // 对自定义类型进行类型推导

auto不光有以上的应用,它在模板中也是大显身手,比如下例这个加工产品的例子中,如果不使用auto就必须声明Product这一模板参数:

  1. template <typename Product, typename Creator>
  2. void processProduct(const Creator& creator) {
  3. Product* val = creator.makeObject();
  4. // do somthing with val
  5. }
  6. .

如果使用auto,则可以这样写:

  1. template <typename Creator>
  2. void processProduct(const Creator& creator) {
  3. auto val = creator.makeObject();
  4. // do somthing with val
  5. }

抛弃了麻烦的模板参数,整个代码变得更加正解了。

 智能指针

std::auto_ptr<TStringList>sl(new TStringList());

decltype

decltype实际上有点像auto的反函数,auto可以让你声明一个变量,而decltype则可以从一个变量或表达式中得到类型,有实例如下:

  1. int x = 3;
  2. decltype(x) y = x;

有人会问,decltype的实用之处在哪里呢,我们接着上边的例子继续说下去,如果上文中的加工产品的例子中我们想把产品作为返回值该怎么办呢?我们可以这样写:

  1. template <typename Creator>
  2. auto processProduct(const Creator& creator) -> decltype(creator.makeObject()) {
  3. auto val = creator.makeObject();
  4. // do somthing with val
  5. }

nullptr

nullptr是为了解决原来C++中NULL的二义性问题而引进的一种新的类型,因为NULL实际上代表的是0,

  1. void F(int a){
  2. cout<<a<<endl;
  3. }
  4. void F(int *p){
  5. assert(p != NULL);
  6. cout<< p <<endl;
  7. }
  8. int main(){
  9. int *p = nullptr;
  10. int *q = NULL;
  11. bool equal = ( p == q ); // equal的值为true,说明p和q都是空指针
  12. int a = nullptr; // 编译失败,nullptr不能转型为int
  13. F(0); // 在C++98中编译失败,有二义性;在C++11中调用F(int)
  14. F(nullptr);
  15. return 0;
  16. }

序列for循环

在C++中for循环可以使用类似java的简化的for循环,可以用于遍历数组,容器,string以及由begin和end函数定义的序列(即有Iterator),示例代码如下:

  1. map<string, int> m{{"a", 1}, {"b", 2}, {"c", 3}};
  2. for (auto p : m){
  3. cout<<p.first<<" : "<<p.second<<endl;
  4. }

Lambda表达式

lambda表达式类似Javascript中的闭包,它可以用于创建并定义匿名的函数对象,以简化编程工作。Lambda的语法如下:

[函数对象参数](操作符重载函数参数)->返回值类型{函数体}

  1. vector<int> iv{5, 4, 3, 2, 1};
  2. int a = 2, b = 1;
  3. for_each(iv.begin(), iv.end(), [b](int &x){cout<<(x + b)<<endl;}); // (1)
  4. for_each(iv.begin(), iv.end(), [=](int &x){x *= (a + b);});     // (2)
  5. for_each(iv.begin(), iv.end(), [=](int &x)->int{return x * (a + b);});// (3)
  • []内的参数指的是Lambda表达式可以取得的全局变量。(1)函数中的b就是指函数可以得到在Lambda表达式外的全局变量,如果在[]中传入=的话,即是可以取得所有的外部变量,如(2)和(3)Lambda表达式
  • ()内的参数是每次调用函数时传入的参数
  • ->后加上的是Lambda表达式返回值的类型,如(3)中返回了一个int类型的变量

变长参数的模板

我们在C++中都用过pair,pair可以使用make_pair构造,构造一个包含两种不同类型的数据的容器。比如,如下代码:

  1. auto p = make_pair(1, "C++ 11");

由于在C++11中引入了变长参数模板,所以发明了新的数据类型:tuple,tuple是一个N元组,可以传入1个, 2个甚至多个不同类型的数据

  1. auto t1 = make_tuple(1, 2.0, "C++ 11");
  2. auto t2 = make_tuple(1, 2.0, "C++ 11", {1, 0, 2});

这样就避免了从前的pair中嵌套pair的丑陋做法,使得代码更加整洁

另一个经常见到的例子是Print函数,在C语言中printf可以传入多个参数,在C++11中,我们可以用变长参数模板实现更简洁的Print

  1. template<typename head, typename... tail>
  2. void Print(Head head, typename... tail) {
  3. cout<< head <<endl;
  4. Print(tail...);
  5. }

Print中可以传入多个不同种类的参数,如下:

  1. Print(1, 1.0, "C++11");

更加优雅的初始化方法

在引入C++11之前,只有数组能使用初始化列表,其他容器想要使用初始化列表,只能用以下方法:

  1. int arr[3] = {1, 2, 3}
  2. vector<int> v(arr, arr + 3);

在C++11中,我们可以使用以下语法来进行替换:

  1. int arr[3]{1, 2, 3};
  2. vector<int> iv{1, 2, 3};
  3. map<int, string>{{1, "a"}, {2, "b"}};
  4. string str{"Hello World"};

然后呢…

如果你想了解更多C++11令人兴奋的新特性,我会向你推荐这两个博客:

胡健的C++11系列博文

ToWrting的C++11系列博文

C++11的编译器支持列表

原文链接:http://my.oschina.net/wangxuanyihaha/blog/183151

http://docwiki.embarcadero.com/RADStudio/XE7/en/Unicode_Character_Types_and_Literals_%28C%2B%2B0x%29

Character Types char16_t and char32_t

Character Literals u'character' and U'character'

String Literals u"UTF-16_string" and U"UTF-32_string"

  • u"UTF-16_string" is a string literal containing characters of the char16_t type, for example u"string_containing_UTF-16_encoding_characters".
  • U"UTF-32_string" is a string literal containing characters of the char32_t type, for example U"string_containing_UTF-32_encoding_characters".

    Range-based for example

char array[] = {'c', '+', '+', '1', '1'};

for (char& i : array)
printf("%c", i);
 
 
c++Builder __property 
 
TDataSet * __fastcall GetCurDataSet(void);
__property TDataSet * curds =       {read = GetCurDataSet};
 
数组
delphi
property IsBlack[Row, Column: Integer]: Boolean read GetIsBlack;
c++
__property bool IsBlack[int Row][int Column] = {read=GetIsBlack};
 
(static_castint>(_width/2)+1)
 
动态数组
System::DynamicArray<System::Byte>  Bookmark
 
System::DynamicArray<int> array { 1, 2, 3, 4, 5};
  1. DynamicArray<int> aint;
    aint.set_length(10);
  2. aint.Low;
  3. aint.High;
  4. aint[0];
    DynamicArray<TBytes> blist; //二维数组
    byte a[10][8];//二维数组

Note: The StaticArray class is designed for Delphi functions that return static arrays.

  1. StaticArray<int, 5>a; a[0] = 100;
  2.  
  3. include\windows\rtl\sysdyn.h
  1. #if defined(GENERIC_ARRAY_NAMES)
  2. typedef DynamicArray<int> ArrayOfint;
  3. typedef DynamicArray<AnsiString> ArrayOfstring;
  4. typedef DynamicArray<WideString> ArrayOfwidestring;
  5. typedef DynamicArray<Byte> ArrayOfbyte;
  6. typedef DynamicArray<short> ArrayOfshort;
  7. typedef DynamicArray<long> ArrayOflong;
  8. typedef DynamicArray<bool> ArrayOfboolean;
  9. typedef DynamicArray<double> ArrayOfdouble;
  10. #endif
  11.  
  12. typedef DynamicArray<Integer> TIntegerDynArray;
  13. typedef DynamicArray<Cardinal> TCardinalDynArray;
  14. typedef DynamicArray<Word> TWordDynArray;
  15. typedef DynamicArray<Smallint> TSmallIntDynArray;
  16. typedef DynamicArray<Byte> TByteDynArray;
  17. typedef DynamicArray<Int8> TInt8DynArray;
  18. typedef DynamicArray<Int8> TShortIntDynArray _DEPRECATED_ATTRIBUTE0;
  19. typedef DynamicArray<__int64> TInt64DynArray;
  20. // NOTE: The following is not quite correct given that
  21. // LongWord is defined as 'unsigned long' in SYSMAC.H
  22. // However, 'DynamicArray<unsigned>' is what the Pascal
  23. // compiler is emitting in .HPPs for TLongWordDynArray
  24. // Please update this if the .HPPs change.
  25. typedef DynamicArray<unsigned> TLongWordDynArray;
  26. typedef DynamicArray<Single> TSingleDynArray;
  27. typedef DynamicArray<Double> TDoubleDynArray;
  28. typedef DynamicArray<Boolean> TBooleanDynArray;
  29. typedef DynamicArray<String> TStringDynArray;
  30. typedef DynamicArray<WideString> TWideStringDynArray;
  1. include\windows\rtl\System.SysUtils.hpp
  1. typedef System::DynamicArray<System::Byte> TBytes;
  2. typedef System::DynamicArray<System::WideChar> TCharArray;
  1. InheritsFrom 继承关系
  1.   this->ActiveControl->InheritsFrom(__classid(TCustomEdit))

c++builder XE7 C++11 C++0x 新语法的更多相关文章

  1. 一起学习c++11——c++11中的新语法

    c++11新语法1: auto关键字 c++11 添加的最有用的一个特性应该就是auto关键字. 不知道大家有没有写过这样的代码: std::map<std::string, std::vect ...

  2. 微信小程序0.11.122100版本新功能解析

    微信小程序0.11.122100版本新功能解析   新版本就不再吐槽了,整的自己跟个愤青似的.人老了,喷不动了,把机会留给年轻人吧.下午随着新版本开放,微信居然破天荒的开放了开发者论坛.我很是担心官方 ...

  3. Mysql8.0.11简介,新特性

    MySQL 8.0 正式版 8.0.11 已发布,官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍,还带来了大量的改进和更快的性能! 注意:从 MySQL 5.7 升级到 MySQL 8 ...

  4. (数据科学学习手札139)geopandas 0.11版本重要新特性一览

    本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 大家好我是费老师,就在几天前,geopandas ...

  5. 2019.11.18【每天学点SAP小知识】Day4 - ABAP 7.40新语法 FOR

    "今天学习一下FOR的语法,常用的2个语法. FOR wa|<fs> IN itab [INDEX INTO idx] [cond] "FOR i = … [THEN ...

  6. 2019.11.10【每天学点SAP小知识】Day3 - ABAP 7.40新语法 值转化和值赋值

    1.语法为 CONV dTYPE|#(...)\ # 代表任意类型 "7.40之前表达式 . DATA helper TYPE string. DATA xstr TYPE xstring. ...

  7. 2019.11.07【每天学点SAP小知识】Day2 - ABAP 7.40新语法 - 内表

    今天学习一下内表的表达式在ABAP 7.4之后的语法: SELECT * FROM mara INTO TABLE @DATA(gt_mara)UP TO 10 ROWS. DATA gt_mara_ ...

  8. 2019.11.06 【每天学点SAP小知识】Day1 - ABAP 7.40新语法

    最近看同事使用ABAP新语法贼溜,省了好多的功夫,还在使用老语法的我眼红了. 所以就自己补一补7.40之后语法,能够让自己写代码更顺畅吧. 今天学习内联申明 inline 意思是:当编译器发现某段代码 ...

  9. C++11带来的优雅语法

    C++11带来的优雅语法 自动类型推导 auto auto的自动类型推导,用于从初始化表达式中推断出变量的数据类型.通过auto的自动类型推导,可以简化我们的编程工作; auto是在编译时对变量进行了 ...

随机推荐

  1. FP-growth算法发现频繁项集(二)——发现频繁项集

    上篇介绍了如何构建FP树,FP树的每条路径都满足最小支持度,我们需要做的是在一条路径上寻找到更多的关联关系. 抽取条件模式基 首先从FP树头指针表中的单个频繁元素项开始.对于每一个元素项,获得其对应的 ...

  2. 【转】每天一个linux命令(46):vmstat命令

    原文网址:http://www.cnblogs.com/peida/archive/2012/12/25/2833108.html vmstat是Virtual Meomory Statistics( ...

  3. shell教程-002:常见的Shell种类

    Shell是一种脚本语言,那么,就必须有解释器来执行这些脚本. Unix/Linux上常见的Shell脚本解释器有bash.sh.csh.ksh等,习惯上把它们称作一种Shell.我们常说有多少种Sh ...

  4. ML(1): 入门理论

    机器学习相关的文章太多,选取一篇本人认为最赞的,copy文章中部分经典供自己学习,摘抄至 http://www.cnblogs.com/subconscious/p/4107357.html#firs ...

  5. 开启postgresql的远程权限

    cd /etc/postxxxx/版本号/main vim postgresql.conf 修改 #listen_addresses ='localhost'为 listen_addresses =' ...

  6. java 解析pdm文档

    前面展示了pdm 的xml结构,既然知道了结构,用java来解析也不会太难,这就为代码自动生成奠定了基础 package com.core.reader.pdmreader.imp; import j ...

  7. Sigar简介

    大家好,我是Sigar.也许好多人还不认识我.下面就介绍一下我自己,好让大家对我有一个大致的了解. 我的全名是System Information Gatherer And Reporter,中文名是 ...

  8. Program.cs 累积_C#

    using System; using System.Diagnostics; using System.Threading; using System.Windows.Forms; using Ut ...

  9. 1077 Kuchiguse (20 分)

    1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Personal ...

  10. nginx二进制编译-启动脚本编写

    首先先把这个文件上传到root目录下,并解压 #tar zxf nginx-1.11.2.tar.gz 写脚本 # vi nginx-running.sh 内容如下 #!/bin/bash #chkc ...