さらに最新のC ++

「C ++ 11は新しい言語のように感じます。」-Bjarne Stroustrup

少し前に、 Herb Sutterは彼のWebサイトに新しいページを開きました。Elementsof Modern C ++ Styleで、新しい標準の利点とコードへの影響について説明しています。

C ++ 11標準には、多くの新機能が導入されています。 さらに、C ++ 11はC ++ 98と比較して新しい言語と見なすことができるため、それらを検討します:

C ++ 11の他の機能を自由に使用してください。 しかし、まず第一に、以下のリストに注意してください。C++ 11でクリーンで安全で高速なコードを書くことができるのは彼らのおかげです-他の現代言語の場合と同様にクリーンで安全です。 さて、C ++の従来のパフォーマンスは、いつものように最高です。

オート


可能な限りautoキーワードを使用してください。
まず、私たちとコンパイラーがすでに知っている型名を繰り返さないでください:
// C++98
map<int,string>::iterator i = m.begin();
 
// C++11
auto i = begin(m);


-, , , .
// C++98
binder2nd< greater<int> > x = bind2nd( greater<int>(), 42 );
 
// C++11
auto x = [](int i) { return i > 42; };


, auto . - , - . , .

- auto, , , . , — . auto: , ( , , , ).

: delete


. delete ( , ).

— shared_ptr, .

// C++98
widget* pw = new widget();
:::
delete pw;
 
// C++11
auto pw = make_shared<widget>();


(, ), weak_ptr.
// C++11
class gadget;
 
class widget {
private:
    shared_ptr<gadget> g;
};
 
class gadget {
private:
    weak_ptr<widget> w;
};


, , unique_ptr, . “new T” , . unique_ptr.
// C++11 Pimpl Idiom
class widget {
    :::
private:
    class impl;
    unique_ptr<impl> pimpl;
};
 
//  .cpp 
class impl {
    :::
};
 
widget::widget()
    : pimpl( new impl() )
{
}


, , .
// C++11
class node {
 vector< unique_ptr<node> > children;
 node* parent;
public:
 :::
};


nullptr


nullptr 0 NULL, , .
// C++98
int* p = 0;
 
// C++11
int* p = nullptr;


for range


for', range, .
// C++98
for( vector<double>::iterator i = v.begin(); i != v.end(); ++i ) {
    total += *i;
}
 
// C++11
for( auto d : v ) {
    total += d;
}


begin end


begin end , , , STL .begin() .end().

-STL , , .begin() .end(), begin end , , STL. , , C.
vector<int> v;
int a[100];
 
// C++98
sort( v.begin(), v.end() );
sort( &a[0], &a[0] + sizeof(a)/sizeof(a[0]) );
 
// C++11
sort( begin(v), end(v) );
sort( begin(a), end(a) );



. STL 100. - (PPL), (C++ AMP).

: v, x y.
// C++98:    ( std::find_if   )
vector<int>::iterator i = v.begin(); // i   
for( ; i != v.end(); ++i ) {
    if( *i > x && *i < y ) break;
}
 
// C++11:  std::find_if
auto i = find_if( begin(v), end(v), [=](int i) { return i > x && i < y; } );
</
source>

    - ,    ?   —      ,  ,       ,       ,     , ..      .
<source>// C#
lock( mut_x ) {
    ... use x ...
}

// C++11 without lambdas: already nice, and more flexible (e.g., can use timeouts, other options)
{
    lock_guard<mutex> hold { mut_x };
    ... use x ...
}

// C++11 with lambdas, and a helper algorithm: C# syntax in C++
// Algorithm: template<typename T> void lock( T& t, F f ) { lock_guard hold(t); f(); }
lock( mut_x, [&]{
    ... use x ...
});


. . Lambdas, Lambdas Everywhere PDC 2010.

Move / &&


API. .
// C++98:   
vector<int>* make_big_vector(); //  1:  :  ,     delete
:::
vector<int>* result = make_big_vector();
void make_big_vector( vector<int>& out ); //  2:   :  ,     
:::
vector<int> result;
make_big_vector( result );
 
// C++11: 
vector<int> make_big_vector();
:::
vector<int> result = make_big_vector();


- , , .


: , POD auto, { } .
// C++98 or C++11
int a = 42;        //  , 

// C++ 11
auto x = begin(v); //   


, ( ) , . : ( float int), POD . C++98: , -
// C++98
rectangle       w( origin(), extents() );   // oops, declares a function, if origin and extents are types
complex<double> c( 2.71828, 3.14159 );
int             a[] = { 1, 2, 3, 4 };
vector<int>     v;
for( int i = 1; i <= 4; ++i ) v.push_back(i);

// C++11
rectangle       w = { origin(), extent() }; // = is optional but I personally prefer it
complex<double> c = { 2.71828, 3.14159 };   // = is optional but I personally prefer it
int             a[] = { 1, 2, 3, 4 };
vector<int>     v = { 1, 2, 3, 4 };


{ } :
// C++98
X::X( /*...*/ ) : mem1(init1), mem2(init2, init3) { /*...*/ }

// C++11
X::X( /*...*/ ) : mem1{init1}, mem2{init2, init3} { /*...*/ }


, :
void draw_rect( rectangle );


// C++98
draw_rect( rectangle( myobj.origin, selection.extents ) );

// C++11
draw_rect( { myobj.origin, selection.extents } );



C++ . . .

, — must-know. C++, . C++ . C++ , — , .

Source: https://habr.com/ru/post/J131478/


All Articles