zoukankan      html  css  js  c++  java
  • c++builder XE7 C++11 C++0x 新语法

    Non-static data member initializers

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

    As a simple example,

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

        class A {
        public:
            int a = 7;
    String aName = "MyName"; };

    would be equivalent to

        class A {
        public:
            A() : a(7) {}
        };
    
    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


    XE764对11支持的全面,32很少。
    http://docwiki.embarcadero.com/RADStudio/XE7/en/C%2B%2B11_Language_Features_Compliance_Status

    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:

    int* func(){}
     
    int _tmain(int argc, _TCHAR* argv[])
    {
            auto x = 3, * y = func(), z = 4;
            return 0;
    }
    32位支持这样初始化
    class TNoteJSON
    {
    public:
     struct TNames
     {
      static String Title;
      static String Content;
      static String Id;
    }
    }

    String TNoteJSON::TNames::Title = "title";
    String TNoteJSON::TNames::Content = "content";
    String TNoteJSON::TNames::Id = "id";

    Caption = TNoteJSON::TNames::Title;
    TNoteJSON  tc;
    Caption = tc.at.Title;
    Caption = tc.at.Content;

    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  

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

     智能指针

    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  

    nullptr

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

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

    序列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;  

    Lambda表达式

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

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

    1. vector<int> iv{5, 4, 3, 2, 1};  
    2. int a = 2, b = 1;  
    3.  
    4. for_each(iv.begin(), iv.end(), [b](int &x){cout<<(x + b)<<endl;}); // (1)  
    5.  
    6. for_each(iv.begin(), iv.end(), [=](int &x){x *= (a + b);});     // (2)  
    7.  
    8. 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...);  

    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};
    DynamicArray<int> aint;
    aint.set_length(10); aint.Low; aint.High; aint[0];
    DynamicArray<TBytes> blist; //二维数组
    byte a[10][8];//二维数组
    Note: The StaticArray class is designed for Delphi functions that return static arrays.

    StaticArray<int, 5>a; a[0] = 100;

    includewindows tlsysdyn.h
    #if defined(GENERIC_ARRAY_NAMES)
      typedef DynamicArray<int>             ArrayOfint;
      typedef DynamicArray<AnsiString>      ArrayOfstring;
      typedef DynamicArray<WideString>      ArrayOfwidestring;
      typedef DynamicArray<Byte>            ArrayOfbyte;
      typedef DynamicArray<short>           ArrayOfshort;
      typedef DynamicArray<long>            ArrayOflong;
      typedef DynamicArray<bool>            ArrayOfboolean;
      typedef DynamicArray<double>          ArrayOfdouble;
    #endif
    
      typedef DynamicArray<Integer>         TIntegerDynArray;
      typedef DynamicArray<Cardinal>        TCardinalDynArray;
      typedef DynamicArray<Word>            TWordDynArray;
      typedef DynamicArray<Smallint>        TSmallIntDynArray;
      typedef DynamicArray<Byte>            TByteDynArray;
      typedef DynamicArray<Int8>            TInt8DynArray;
      typedef DynamicArray<Int8>            TShortIntDynArray _DEPRECATED_ATTRIBUTE0;
      typedef DynamicArray<__int64>         TInt64DynArray;
      // NOTE: The following is not quite correct given that
      //       LongWord is defined as 'unsigned long' in SYSMAC.H
      // However, 'DynamicArray<unsigned>' is what the Pascal
      // compiler is emitting in .HPPs for TLongWordDynArray
      // Please update this if the .HPPs change.
      typedef DynamicArray<unsigned>        TLongWordDynArray;
      typedef DynamicArray<Single>          TSingleDynArray;
      typedef DynamicArray<Double>          TDoubleDynArray;
      typedef DynamicArray<Boolean>         TBooleanDynArray;
      typedef DynamicArray<String>          TStringDynArray;
      typedef DynamicArray<WideString>      TWideStringDynArray;
    includewindows
    tlSystem.SysUtils.hpp
    typedef System::DynamicArray<System::Byte> TBytes;
    typedef System::DynamicArray<System::WideChar> TCharArray;
    
    InheritsFrom 继承关系
      this->ActiveControl->InheritsFrom(__classid(TCustomEdit)) 
  • 相关阅读:
    【WCF】服务并发中的“可重入模式”
    【.net 深呼吸】项目中是否有必要删去多余的引用
    【.net 深呼吸】细说CodeDom(10):生成异常处理语句
    【.net 深呼吸】细说CodeDom(9):动态编译
    【.net 深呼吸】细说CodeDom(8):分支与循环
    shell脚本将gbk文件转化为utf-8
    PHP判断文件大小是MB、GB、TB...
    svn: E205007: None of the environment variables SVN_EDITOR
    phpstorm yii2框架的redis和mongodb提示
    linux下phpmailer发送邮件出现SMTP ERROR: Failed to connect to server: (0)错误
  • 原文地址:https://www.cnblogs.com/cb168/p/3983302.html
Copyright © 2011-2022 走看看