C++ 11 介绍 —— 基础概念

C++的基本目的是为了兼容C,因此C++11是基于C99的(ISO/IEC 9899:1999),C++主要是在其基础上扩展了一些特性,这包括基础数据类型(bool)、类(class)、模板(template)、异常(exception)、名字空间(namespace)、操作符重载(operator +)、函数名重载(function name mangle)、引用(&)、自由存储管理操作符(new/delete)以及包含一套C++标准库。

 

C++11标准遵循下列标准。

  1. Ecma International, ECMAScript Language Specification, Standard Ecma-262, third edition, 1999
  2. ISO/IEC 2382 (all parts), Information Technology -- Vocabulary
  3. ISO/IEC 9899:1999, Programming Languages -- C
  4. ISO/IEC 9899:1999/Cor.1:2001(E), Programming Languages -- C, Technical Corrigendum 1
  5. ISO/IEC 9899:1999/Cor.2:2004(E), Programming Languages -- C, Technical Corrigendum 2
  6. ISO/IEC 9899:1999/Cor.3:2007(E), Programming Languages -- C, Technical Corrigendum 3
  7. ISO/IEC 9945:2003, Information Technology -- Portable Operating System Interface (POSIX)
  8. ISO/IEC 10646-1:1993, Information Technology — Universal Multiple-Octet Coded Character Set (UCS)
  9.     -- Part 1: Architecture and Basic Multilingual Plane
  10. ISO/IEC TR 19769:2004, Information Technology — Programming Languages, their environments and
              system software interfaces — Extensions for the programming language C to support new character
              data types

 

在C++11中,表达式范畴总是在glvalue(generalized left-hand value)、rvalue(right-hand value)、lvalue(left-hand value)、xvalue(expiring value)和prvalue(pure right-hand value)这些范畴之中,其关系表示如下:

                       expression

                       /           \

                glvalue         rvalue

                /        \       /       \

          lvalue        xvalue       prvalue

在C++03中的lvalue仍然在C++0x中称之为lvalue,但是rvalue现在称之为prvalue。

 

Token

C++包含5种类型的Token,分别是标识符(identifiers)、关键字(keywords)、文字常量(literals)、操作符(operators)以及分隔符(separators)。

 

Comment

C++支持两种风格的注释,分别是以字符/*开始并以*/结束的注释,这些注释不能被嵌套,但可以跨越多行,以字符//开始并以换行符结束的注释。例如:

   1: // This is comments
   2: int main(int argc, char* argv[]) {
   3:     /* This
   4:        is
   5:        comments
   6:     */
   7:     return 0;
   8: }

 

Header Name

头文件名是一个预处理token,他们仅出现在#include预处理指示符之后,其中标准库头文件以<>包围,而自定义头文件以""包围。例如:

   1: #include <iostream> // This is standard header file
   2: /** This is custom header file
   3:  *  that defined const char* str = "Hello World";
   4:  */ 
   5: #include "hello.h"
   6:  
   7: int main(int argc, char* argv[]) {
   8:     std::cout << str << std::endl;
   9:     return 0;
  10: }

在GCC中,以<>包含的头文件会被认为为系统头文件,它在标准系统目录中搜索对应的头文件,你能够使用-isystem包含自定义目录;以""包含的头文件会被认为为用户头文件,其首先会在包含当前文件的目录中搜索对应头文件,然后使用<>相同的目录,你能够使用-iquote包含自定义目录。

 

Identifier

标识符有是任意长度的数字与字母序列,其中开始字母不能为数字。合法的字母与数字如下:

字母:[a-z] [A-Z] _

数字:[0-9]

另外,有一部分标识符由C++实现与标准库保留,我们应该尽可能避免使用它们。包含双下划线__或以单下划线_开始并紧接着跟随一个大写字母的标识符被保留;

 

Keywords

下面列出了C++11关键字全集,其中export关键字并未使用但保留为将来使用。

alignasalignofasmautoboolbreakcasecatchchar
char16_tchar32_tclassconstconstexprconst_castcontinuedecltypedefault
deletedodoubledynamic_castelseenumexplicitexportextern
falsefloatforfriendgotoifinlineintlong
mutablenamespacenewnoexceptnullptroperatorprivateprotectedpublic
registerreinterpret_castreturnshortsignedsizeofstaticstatic_assertstatic_cast
structswitchtemplatethisthread_localthrowtruetrytypedef
typeidtypenameunionunsignedusingvirtualvoidvolatilewchar_t
while        

 

Operators and Punctuators

{  }  [  ]  #  ##  (  )  <:  :>  <%  %>  %:  %:%:  ;  :  ...  new  delete  ?  ::  .  .*  +  -  *  /  %  ˆ  &  |  ~  !  =  <  > 

+=  -=  *=  /=  %=  ˆ=  &=  |=  <<  >>  >>=  <<=  ==  !=  <=  >=  &&  ||  ++  --  ,  ->*  -> 

and  and_eq  bitand  bitor  compl  not  not_eq  or  or_eq  xor  xor_eq

 

Literals

文字常量包含整形文字常量、字符文字常量、浮点文字常量、字符串文字常量、布尔文字常量、指针位置常量、用户定义的文字常量

 

Integer literals

整形文字常量有3中表示形式,分别是十进制、八进制与十六进制表示。比如:

   1: int ival = 12; long lval = 12L; long long llval = 12LL;
   2: int oival = 014; long olval = 014L; long long ollval = 014LL;
   3: int xival = 0xc; long xlval = 0xcL; long long xllval = 0xcLL;
   4:  
   5: unsigned uival = 12U; unsigned long ulval = 12UL; unsigned long long ullval = 12ULL;
   6: unsigned ouival = 014U; unsigned long oulval = 014UL; unsigned long long oullval = 014ULL;
   7: unsigned xuival = 0xcU; unsigned long xulval = 0xcUL; unsigned long long xullval = 0xcULL;

 相对于C++03增加了long long标准类型,其实很早之前个编译器已经支持这个类型。

 

Character literals

字符常量有四种表示形式,分别是char, wchar_t, char16_t, char32_t。例如:

   1: char ch = 'a'; wchar_t wch = L'a'; char16_t ch16 = u'a'; char32_t ch32 = U'a';

 

Floating literals

例如:

   1: float fval = 3.14F; float fval_ = .314e1F;
   2: double dval = 40.17; double dval_ = 4.017e1;
   3: long double ldval = 88.789L; long double ldval_ = .88789e2L;

 

String literals

字符串文字常量是一个有双引号括起来的字符序列,共有10种表示形式。其中,在C++11中引入RAW String,所有以R为前缀并包含界定符(delimiter)的字符串即为raw字符串。比如:

   1: const char* str = "hello world"; // narrow-char string literal.
   2: const wchar_t* wstr = L"hello world"; // wchar_t string literal
   3: const char16_t* str16 = u"hello world"; // char16_t string literal
   4: const char32_t* str32 = U"hello world"; // char32_t string literal
   5:  
   6: const char* utf8_str = u8"hello world"; // utf-8 string
   1: const char* raw_str = R"(hello world)";
   2: const wchar_t* raw_wstr = LR"(hello world)";
   3: const char16_t* raw_str16 = uR"(hello world)";
   4: const char32_t* raw_str32 = UR"(hello world)";
   5:  
   6: const char* raw_utf8_str = u8R"(hello world)";

 

Boolean literals

布尔文字常量是关键字true和false。

 

Pointer literals

指针文字常量是关键字nullptr,它的类型是std::nullptr_t,而不是整型值。例如:

   1: int* ptr = nullptr;

 

User-defined literals

C++11新增加用户定义的文字常量,例如:

一个用户定义的文字常量被视为调用一个文字常量操作符或文字常量操作符模板,注意。例如:

   1: long double operator "" _w(long double);
   2: std::string operator "" _w(const char16_t*, size_t);
   3: unsigned operator "" _w(const char*);
   4:  
   5: int main() {
   6:     1.2_w; // call operator "" _w(1.2L), type is long double
   7:     u"one"_w; // call operator "" _w(u"one", 3), type is std::string
   8:     12_w; // call operator "" _w("12"), type is unsigned
   9:     "two"_w; // error: no applicable literal operator
  10:  
  11:     return 0;
  12: }

转载于:https://www.cnblogs.com/chee-z/archive/2012/07/15/2592349.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值