设为首页 加入收藏

TOP

????????????10??C++11???
2013-09-26 19:51:22 来源: 作者: 【 】 浏览:102
Tags:

 

unique_ptr shared_ptr http://stackoverflow.com/questions/15648844/using-smart-pointers-for- class-members

unique_ptr unique_ptr std::move( ) get() nullptr

void foo(int* p) { std::cout << *p << std::endl; } std::unique_ptr p1(new int(42)); std::unique_ptr p2 = std::move(p1); // transfer ownership if(p1) foo(p1.get()); (*p2)++; if(p2) foo(p2.get());

shared_ptr ÷ 岻

void foo(int* p) { } void bar(std::shared_ptr p) { ++(*p); } std::shared_ptr p1(new int(42)); std::shared_ptr p2 = p1; bar(p1); foo(p2.get());

auto p3 = std::make_shared(42);

make_shared 乲 档 shared_ptr й У seed() й

void foo(std::shared_ptr p, int init) { *p = init; } foo(std::shared_ptr(new int(42)), seed());

make_shared weak_ptr lock() shared_ptr

auto p = std::make_shared(42); std::weak_ptr wp = p; { auto sp = wp.lock(); std::cout << *sp << std::endl; } p.reset(); if(wp.expired()) std::cout << "expired" << std::endl;

(lock) ( ) weak_ptr shared_ptr.

Lambdas

( lambda) C++(www.cppentry.com) У н κ ú (functor) std::function lambda (http://msdn.microsoft.com/en-us/library/dd293603.aspx)

std::vector v; v.push_back(1); v.push_back(2); v.push_back(3); std::for_each(std::begin(v), std::end(v), [](int n) {std::cout << n << std::endl;}); auto is_odd = [](int n) {return n%2==1;}; auto pos = std::find_if(std::begin(v), std::end(v), is_odd); if(pos != std::end(v)) std::cout << *pos << std::endl;

lambda Fibonacci lambda auto

auto fib = [&fib](int n) {return n < 2 1 : fib(n-1) + fib(n-2);};

error C3533: 'auto &': a parameter cannot have a type that contains 'auto' error C3531: 'fib': a symbol whose type contains 'auto' must have an initializer error C3536: 'fib': cannot be used before it is initialized error C2064: term does not evaluate to a function taking 1 arguments

auto ζ 顣 std::function

std::function lfib = [&lfib](int n) {return n < 2 1 : lfib(n-1) + lfib(n-2);};

begin() end()

begin() end() ÷ е STL κ C

е std::vector 滻 C 顣

int arr[] = {1,2,3}; std::for_each(&arr[0], &arr[0]+sizeof(arr)/sizeof(arr[0]), [](int n) {std::cout << n << std::endl;}); auto is_odd = [](int n) {return n%2==1;}; auto begin = &arr[0]; auto end = &arr[0]+sizeof(arr)/sizeof(arr[0]); auto pos = std::find_if(begin, end, is_odd); if(pos != end) std::cout << *pos << std::endl;

÷ begin() end()

int arr[] = {1,2,3}; std::for_each(std::begin(arr), std::end(arr), [](int n) {std::cout << n << std::endl;}); auto is_odd = [](int n) {return n%2==1;}; auto pos = std::find_if(std::begin(arr), std::end(arr), is_odd); if(pos != std::end(arr)) std::cout << *pos << std::endl;

std::vecto ζ д begin() end()

template void bar(Iterator begin, Iterator end) { std::for_each(begin, end, [](int n) {std::cout << n << std::endl;}); auto is_odd = [](int n) {return n%2==1;}; auto pos = std::find_if(begin, end, is_odd); if(pos != end) std::cout << *pos << std::endl; } template void foo(C c) { bar(std::begin(c), std::end(c)); } template void foo(T(&arr)[N]) { bar(std::begin(arr), std::end(arr)); } int arr[] = {1,2,3}; foo(arr); std::vector v; v.push_back(1); v.push_back(2); v.push_back(3); foo(v);

static_assert type traits

static_assert 顣 棬

template class Vector { static_assert(Size < 3, "Size is too small"); T _points[Size]; }; int main() { Vector a1; Vector a2; return 0; }

error C2338: Size is too small see reference to class template instantiation 'Vector' being compiled with [ T=double, Size=2 ]

static_assert type traits type traits Щclass п кü class: helper class type traits class о type transformation class 任

δ

template auto add(T1 t1, T2 t2) -> decltype(t1 + t2) { return t1 + t2; }

д

std::cout << add(1, 3.14) << std::endl; std::cout << add("one", 2) << std::endl;

4.14 e н

template auto add(T1 t1, T2 t2) -> decltype(t1 + t2) { static_assert(std::is_integral::value, "Type T1 must be integral"); static_assert(std::is_integral::value, "Type T2 must be integral"); return t1 + t2; }

error C2338: Type T2 must be integral see reference to function template instantiation 'T2 add(T1,T2)' being compiled with [ T2=double, T1=int ] error C2338: Type T1 must be integral see reference to function template instantiation 'T1 add(T1,T2)' being compiled with [ T1=const char *, T2=int ]

Move semantics (Move )

C++(www.cppentry.com)11 д

C++(www.cppentry.com)11 (rvalue reference) ( && ) á ( ) move ( const T& )

C++(www.cppentry.com) class struct Щ ( κ ) п bit-wise ( ) bit Щ

( ) ζ 濽

move constructor move assignment operator T&& 硰 ( vector queue) 飬 а 档

buffer buffer buffer ( ) ( unique_ptr ) T 飬 鳤

template class Buffer { std::string _name; size_t _size; std::unique_ptr _buffer; public: // default constructor Buffer(): _size(16), _buffer(new T[16]) {} // constructor Buffer(const std::string& name, size_t size): _name(name), _size(size), _buffer(new T[size]) {} // copy constructor Buffer(const Buffer& copy): _name(copy._name), _size(copy._size), _buffer(new T[copy._size]) { T* source = copy._buffer.get(); T* dest = _buffer.get(); std::copy(source, source + copy._size, dest); } // copy assignment operator Buffer& operator=(const Buffer& copy) { if(this != ©) { _name = copy._name; if(_size != copy._size) { _buffer = nullptr; _size = copy._size; _buffer = _size > 0 > new T[_size] : nullptr; } T* source = copy._buffer.get(); T* dest = _buffer.get(); std::copy(source, source + copy._size, dest); } return *this; } // move constructor Buffer(Buffer&& temp): _name(std::move(temp._name)), _size(temp._size), _buffer(std::move(temp._buffer)) { temp._buffer = nullptr; temp._size = 0; } // move assignment operator Buffer& operator=(Buffer&& temp) { assert(this != &temp); // assert if this is not a temporary _buffer = nullptr; _size = temp._size; _buffer = std::move(temp._buffer); _name = std::move(temp._name); temp._buffer = nullptr; temp._size = 0; return *this; } }; template Buffer getBuffer(const std::string& name) { Buffer b(name, 128); return b; } int main() { Buffer b1; Buffer b2("buf2", 64); Buffer b3 = b2; Buffer b4 = getBuffer("buf4"); b1 = getBuffer("buf5"); return 0; }

copy constructor copy assignment operator ú C++(www.cppentry.com)11 move constructor move assignment operator move δ b4 move constructor á b1 move assignment operator á getBuffer()

move constuctor е name buffer std::move name string std::string move 塣std::unique_ptr д_name(temp._name) copy constructor á _buffer д std::unique_ptr copy constructor std::string move constructor б Buffer move constructor temp ( move constructor) std::move á

move constructor move assignment operator Ψ Member 7805758 棺

template class Buffer { std::string _name; size_t _size; std::unique_ptr _buffer; public: // constructor Buffer(const std::string& name = "", size_t size = 16): _name(name), _size(size), _buffer(size new T[size] : nullptr) {} // copy constructor Buffer(const Buffer& copy): _name(copy._name), _size(copy._size), _buffer(copy._size new T[copy._size] : nullptr) { T* source = copy._buffer.get(); T* dest = _buffer.get(); std::copy(source, source + copy._size, dest); } // copy assignment operator Buffer& operator=(Buffer copy) { swap(*this, copy); return *this; } // move constructor Buffer(Buffer&& temp):Buffer() { swap(*this, temp); } friend void swap(Buffer& first, Buffer& second) noexcept { using std::swap; swap(first._name , second._name); swap(first._size , second._size); swap(first._buffer, second._buffer); } };

C++(www.cppentry.com)11 к е C++(www.cppentry.com) е

      

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++中const的用法小结 下一篇开发者应知道的10个C++11特性

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: