Вы находитесь на странице: 1из 18

++ C++0x

: : 01.04.2010

Visual Studio 2010.

++, PVS-Studio .

C++0x ,

++ , , , ,

++ . -

. - C++11. ,

C++0x, Intel C++ Visual C++. , .

, . ++ . C++0x. , Visual Studio 2010. . , decltype: , , , PVS-Studio , "stddef.h" . ,

namespace std { typedef decltype(__nullptr) nullptr_t; }

, C++0x, 2010, . , PVS-Studio 3.50, Visual Studio 2010. PVS-Studio 3.50 ++0x, Visual Studio 2010. "right-angle brackets", . Visual Studio 2005/2008, , C++0x , , C++0x PVS-Studio

, . Visual Studio

, Visual Studio 2010. , PVS-Studio VivaCore. . VivaCore PVS-Studio , ++. . VivaCore VivaCore . 64 , :

PVS-Studio. OpenMP . , PVS-Studio 64-

PVS-Studio , .

6464 -

1. auto
++, , . , : . , , , , ++

for (vector<int>::iterator itr = myvec.begin(); itr != myvec.end(); ++itr)

. typedef, C++0x auto . , var C# 3.0. , , auto . , auto, . (register, .

, ),

, auto

auto.

++ . :

, :

for (auto itr = myvec.begin(); itr != myvec.end(); ++itr)

, . 64: , auto

auto

bool Find_Incorrect(const string *arrStr, size_t n)

{ for (size_t i = 0; i != n; ++i) { unsigned n = arrStr[i].find("ABC"); if (n != string::npos) return true; } return false; };

64unsigned "n", find(). 32-

. Win64.

Win32 string::size_type, string::size_type unsigned , string::size_type unsigned find() 0xFFFFFFFFu 0xFFFFFFFFu != Find_Incorrect true.

. 64. string::npos, 0xFFFFFFFFFFFFFFFFui64. 32. 0xFFFFFFFFFFFFFFFFui64 , , Viva64 ( :


warning C4267: 'initializing' :

PVS-Studio).

conversion from 'size_t' to 'unsigned int', possible loss of data

Viva64:
V103: Implicit type conversion from memsize to 32 -bit type.

, .

, string::size_type. . :

"auto"

auto n = arrStr[i].find("ABC"); if (n != string::npos) return true;

. "auto" "auto" . ! 64"auto" , .


void *AllocArray3D(int x, int y, int z, size_t objectSize) { int size = x * y * z * objectSize; return malloc(size); }

. .

64 , :

. 642000*2000*2000 "double". 2000, sizeof(double));" NULL, . , "int size = x * y * z * sizeof(double)". -424509440 malloc :
warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible lo ss of data

"A llocArray3D(2000, 2000, NULL, "size" . ,

"auto",
void *AllocArray3D(int x, int y, int z, size_t objectSize) { auto size = x * y * z * objectSize; return (double *)malloc(size); }

, , "size" "x * y * z". "size_t" AllocArray3D -

. NULL.

"size_t". "int" "objectSize". , Viva64:

V104: Implicit type conversion to memsize type in an arithmetic expression.

"auto", ,

. VivaCore, ,

AA "size_t":

"int",

Viva64. , ( . V101)

void Foo(int X, int Y) { auto AA = X * Y; size_t BB = AA; //V101 }

, C++0x. tkAUTOcpp0x. Lex.cc

, tableC0xx. "auto" (tkAUTO), "auto"

isTypeToken, optIntegralTypeOrClassSpec. - AutoDecltypeType. "auto" , 'x', ,

: LeafAUTOc0xx. TypeInfoId

TypeInfo Encoding.

IsAutoCpp0x, MakePtree. "auto", 'x'). . VivaCore "auto AA = X * Y"

( , "int". ,

, AA

Viva64 VivaCore. TranslateAssignInitializer. (Bind) , .

2. decltype
" , , , "decltype". "Calc()": , "value" , " . "auto" .

decltype(Calc()) value;

try { value = Calc(); } catch(...) { throw; }

"decltype"
void f(const vector<int>& a, vector<float>& b) { typedef decltype(a[0]*b[0]) Tmp; for (int i=0; i<b.size(); ++i) { Tmp* p = new Tmp(a[i]*b[i]); // ... } }

, auto.

decltype,

const std::vector<int> v(1); auto a = v[0];decltype(v[0]) b = 1; // // a - int b - const int& (

// std::vector<int>::operator[](size_type) const)

, IsPresent

"decltype" "true"

64:

bool IsPresent(char *array, size_t arraySize, char key) { for (unsigned i = 0; i < arraySize; i++)

if (array[i] == key) return true; return false; }

64arraySize . "auto",
for (auto i = 0; i < arraySize; i++) if (array[i] == key) return true;

UINT_MAX,

. "i < arraySize"

"i" "decltype":

"int",

"int".

for (decltype(arrayS ize) i = 0; i < arraySize; i++) if (array[i] == key) return true;

"i" "decltype" tkDECLTYPE.

"size_t" VivaCore

"arraySize". "auto". rDecltype Parser.cc. optIntegralTypeOrClassSpec.

LeafDECLTYPE. "decltype" 'x', auto). TypeInfo Encoding. , "decltype" Environment / RecordConstantDeclarator). VivaCore. Environment ( RecordTypedefName, RecordDeclarator, FixIfDecltype. 'X' ( WhatIs, IsDecltype, MakePtree.

'X',

3.
C++98 (const &). , C++0x :" _ . &&". ,

(R-value reference)
, , const &. (R-value reference). ,

(Move semantics). , .

std::vector , std::vector ,

. :

template<class T> class vector { // ... vector(const vector&); // copy constructor vector(vector&&); // move constructor

vector& operator=(const vector&); // copy assignment vector& operator=(vector&&); }; // move assignment

'&' .

64'&&'. optPtrOperator '&', '&&'.

, VivaCore Parser.

4.
C++98
list<vector<string>> lvs;

:
list<vector<string> > lvs;

++0x , . , . , . , , , ">>" "A<B<C>> D": IsTemplateAngleBrackets, isTemplateArgs. , Brackets (N1757)". , , : "Right Angle VivaCore. . ">>" VivaCore .

5.
-

++

(Lambdas)
( ). : . ,

/* callback-

*/

int compare_functio n(int A, int B) { return A < B; }

/* void mysort(int* begin_items, int num_items,

*/

int (*cmpfunc)(int, int));

int main(void) { int items[] = {4, 3, 1, 2}; mysort(items, sizeof(items)/sizeof(int), compare_function); return 0; }

++
class compare_class { public: bool operator()(int A, int B) { return (A < B); } };

operator():

// template <class ComparisonFunctor>

void mysort (int* begin_items, int num_items, ComparisonFunctor c);

int main() { int items[] = {4, 3, 1, 2}; compare_class functor; mysort(items, sizeof(items)/sizeof(int), functor); }

C++0x
auto compare_function = [](char a, char b) { return a < b; }; char Str[] = "cwgaopzq"; std::sort(Str, Str + strlen(Str), compare_function); cout << Str << endl;

compare_function . std::sort.
char Str[] = "cwgaopzq"; std::sort( Str, Str + strlen(Str), [](char a, char b) {return a < b;} ); cout << Str << endl;

"[](char a, char b) {return a < b;}"

[], . :

. .

'[' [< [ '(' <

_ _

>] ']' > ')' ['mutable' ] ] _ _ >] '}' >] ')' ] > ]

[ 'throw' '(' [< [ '->' < '{' [< _ _

. . Visual C++. , :
y y y y y y y y

noexcept,

[] [=] [&] [x, y] x y [&x, &y] x y [in, &out] in [=, &out1, &out2] ; [&, x, &y] , .

; ; ; ; ; , out ; , , x. . , out1 out2,

. . ,
int _tmain(int, _TCHAR*[]) { vector<string> strings; strings.push_back("lambdas"); strings.push_back("decltype"); strings.push_back("auto");

strings.push_back("static_assert"); strings.push_back("nullptr"); vector<size_t> indices; size_t k = 0; generate_n(back_inserter(indices), strings.size(), [&k]() { return k++; }); sort(indices.begin(), indices.end(), [&](ptrdiff_t i1, ptrdiff_t i2) { return strings[i1].length() < strings[i2].length(); }); for_each(indices.begin(), indices.end(), [&strings](const size_t i) { cout << strings[i] << endl; }); return 0; }

++0x

std::vector

vector<size_t> in dices = {0,1,2,3,4};

Visual Studio 2010 . , PVS-Studio ,


int a = -1; unsigned b = 0; const char str[] = "Viva64"; const char *p = str + 1; auto lambdaFoo = [&]() -> char

64: . .

{ return p[a+b]; }; cout << lambdaFoo() << endl;

Win32 Win64 0xFFFFFFFF. / ++ - " 13. -

'V'. 64 -

5.

". :

, PVS-Studio

error V108: Incorrect index type: p[not a memsize -type]. Use memsize type instead.

. Parser rCommaExpression. - PtreeLambda. PtreeLambda. , . VivaCore. rLambdas. rInitializeExpr, rFunctionArguments, PtreeLambda.h

rLambdas PtreeLambda

PtreeLambda VivaCore. GetReturnLambdaFunctionTypeForReturn, PVS-Studio GetReturnLambdaFunctionTypeForReturn VivaCore

TranslateLambda. TranslateLambda PVS-Studio. .

6. Suffix return type syntax


, ,
template<class T, class U> ??? mul(T x, U y) { return x*y; }

, :

"???".
template<class T, class U>

"x*y". , "decltype":

decltype(x*y) mul(T x, U y) //Scope problem! { return x*y; }

"x" "y" .

"decltype(x*y)"

:
template<class T, class U> [] mul(T x, U y) -> decltype(x*y) { return x*y; }

[] ". (
template<class T, class U>

, Visual C++. , Suffix return type syntax):

" ,

auto mul(T x, U y) -> decltype(x*y) { return x*y; }

Visual C++ PVS-Studio 3.50 , Parser::rIntegralDeclaration. VivaCore, . VivaCore PVS-Studio

. .

7. static_assert
C++0x
static_assert( , "

static_assert.
");

, . static_assert:

template <unsigned n> struct MyStruct { static_assert(n > 5, "N must be more 5"); }; MyStruct<3> obj;

Visual C++
error C2338: N must be more 5 xx.cpp(33) : see reference to class template instantiation 'MyStruct<n>' being compiled with [ n=3 ]

, tkSTATIC_ASSERT. static_assert ( ,

PVS-Studio, . VivaCore Lex::ReadToken).

static_assert ,

8. nullptr
C++0x NULL. ++ 0. NULL NULL Visual Studio:
0

. 0, .

#define NULL

.
void Foo(int a) { cout << "Foo(int a)" << endl; } void Foo(char *a) { cout << "Foo(char *a)" << endl; } int _tmain(int, _TCHAR*[]) {

Foo(0); Foo(NULL); return 0; }

, . NULL :
Foo(int a) Foo(int a)

Foo, 0, "int"

C++0x nullptr_t . nullptr "bool". "Foo"


void Foo(int a) { cout << "Foo(int a)" << endl; } void Foo(char *a) { cout << "Foo(char *a)" << endl; } int _tmain(int, _TCHAR*[]) { Foo(0); Foo(NULL); Foo(nullptr); return 0; }

nullptr.

nullptr

nullptr:

:
Foo(int a) Foo(int a) Foo(char *a)

nullptr tkNULLPTR, rPrimaryExpr. LeafNULLPTR. VivaCore LeafNULLPTR LeafNULLPTR::Typeof "nullptr" .

64 -

"Pv",

"void *". .

PVS-Studio

9.
C++0x Visaul Studio 2010.
y y y

namespace std. :

std::array; std::shared_ptr; std::regex. , VivaCore.

PVS-Studio

10.
, , , . , C++0x , . :
{ int x; std::vector<int> A(10); A[0] = x; // Warning C4700 } { int x; std::vector<int> A(10); std::for_each(A.begin(), A.end(), [x](int &y) { y = x; } // No Warning ); }

C++0x. , , .

, . -

Visual Studio 2010 RC, /W4). . , , PVS-Studio 64, C++0x. . ,

C++0x. . .

C++0x , . , C++0x

. . .

1. Bjarne Stroustrup. C++0x - the next ISO C++ standard. http://www.viva64.com/go.php?url=304 2. Visual C++ Team Blog. Rvalue References: C++0x Features in VC10, Part 2. http://www.viva64.com/go.php?url=305 3. . C++0x. . http://www.viva64.com/go.php?url=306 4. . ++0x . http://www.viva64.com/go.php?url=307 5. Wikipedia. C++0x. http://www.viva64.com/go.php?url=301 6. Herb Sutter. Trip Report: March 2010 ISO C++ Standards Meeting. http://www.viva64.com/go.php?url=308