您的位置:首页 > 编程语言 > C语言/C++

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

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

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

Ecma International, ECMAScript Language Specification, Standard Ecma-262, third edition, 1999

ISO/IEC 2382 (all parts), Information Technology -- Vocabulary

ISO/IEC 9899:1999, Programming Languages -- C

ISO/IEC 9899:1999/Cor.1:2001(E), Programming Languages -- C, Technical Corrigendum 1

ISO/IEC 9899:1999/Cor.2:2004(E), Programming Languages -- C, Technical Corrigendum 2

ISO/IEC 9899:1999/Cor.3:2007(E), Programming Languages -- C, Technical Corrigendum 3

ISO/IEC 9945:2003, Information Technology -- Portable Operating System Interface (POSIX)

ISO/IEC 10646-1:1993, Information Technology — Universal Multiple-Octet Coded Character Set (UCS)

-- Part 1: Architecture and Basic Multilingual Plane

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

/\ / \

lvaluexvalue prvalue

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

Token

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

Comment

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

// This is comments


int main(int argc, char* argv[]) {


/* This


is


comments


*/


return 0;


}


Header Name

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

#include <iostream> // This is standard header file


/** This is custom header file


*that defined const char* str = "Hello World";


*/


#include "hello.h"




int main(int argc, char* argv[]) {


 std::cout << str << std::endl;


return 0;


}


在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

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

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

andand_eqbitandbitorcomplnotnot_eqoror_eqxorxor_eq

Literals

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

Integer literals

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

int ival = 12; long lval = 12L; long long llval = 12LL;


int oival = 014; long olval = 014L; long long ollval = 014LL;


int xival = 0xc; long xlval = 0xcL; long long xllval = 0xcLL;




unsigned uival = 12U; unsigned long ulval = 12UL; unsigned long long ullval = 12ULL;


unsigned ouival = 014U; unsigned long oulval = 014UL; unsigned long long oullval = 014ULL;


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。例如:

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


Floating literals

例如:

float fval = 3.14F; float fval_ = .314e1F;


double dval = 40.17; double dval_ = 4.017e1;


long double ldval = 88.789L; long double ldval_ = .88789e2L;


String literals

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

const char* str = "hello world"; // narrow-char string literal.


const wchar_t* wstr = L"hello world"; // wchar_t string literal


const char16_t* str16 = u"hello world"; // char16_t string literal


const char32_t* str32 = U"hello world"; // char32_t string literal




const char* utf8_str = u8"hello world"; // utf-8 string


const char* raw_str = R"(hello world)";


const wchar_t* raw_wstr = LR"(hello world)";


const char16_t* raw_str16 = uR"(hello world)";


const char32_t* raw_str32 = UR"(hello world)";




const char* raw_utf8_str = u8R"(hello world)";


Boolean literals

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

Pointer literals

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

int* ptr = nullptr;


User-defined literals

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

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

long double operator "" _w(long double);


std::string operator "" _w(const char16_t*, size_t);


unsigned operator "" _w(const char*);




int main() {


 1.2_w; // call operator "" _w(1.2L), type is long double


 u"one"_w; // call operator "" _w(u"one", 3), type is std::string


 12_w; // call operator "" _w("12"), type is unsigned


 "two"_w; // error: no applicable literal operator




return 0;


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: