1. 어디다 쓰나?

shared_ptr for shared ownership, unique_ptr for ownership transfer

 

2. custom deleter 지정하기

2.1 shared_ptr의 경우 해당 포인터를 인자로 받는 functor를 constructor의 2번째 인자로 넘겨 준다.

 

std::shared_ptr<int> sp(new int[10], 
    [](int * p) { delete [] p; }); 

 

2.2 unique_ptr의 경우 custom deleter의 타입을 2번째 template argument로 넘겨줘야 한다.

int * GetResource(); 

auto lam = [](int * p) { 
    // do something... 
    Release(p); }; 

std::unique_ptr<int, decltype(lam)> up1(GetResource(), lam); 

// other example 
std::unique_ptr<int, void (*)(int *)> up2(new int[10], [](int * p) { delete [] p; }); 

// other example 
std::unique_ptr<int, std::function<void (int *)>> up3(new int[20],
    [](int * p) { delete [] p; }); 

 

 

3. array를 다룰 때

#include <memory> 

// shared_ptr의 경우 custom deleter로 지정해야 함. 
std::shared_ptr<int> sp(new int[10], 
    [](int * p) { delete [] p; }); 

// unique_ptr의 경우 array를 위한 specialization이 있음. 
std::unique_ptr<int[]> up(new int[10]);

'programming > C++' 카테고리의 다른 글

C++11 cheat sheet  (0) 2013.02.18
Universal reference  (0) 2013.02.18
std::async and std::future 쓸 때...  (0) 2013.02.18
constexpr  (0) 2013.02.18
Uniform initialization in C++11  (0) 2013.02.18
Posted by 무한자전거
,

1. Launch policy 없이 async()를 사용했을 때, std::future::get()(혹은 wait())을 call하지 않으면, 해당 function이 호출된다는 보장이 없다.

#include <future> 

void DoSomething() 
{ 
    // Do something... 
} 

int main() 
{ 
    auto f = std::async([]{ DoSomething(); }); 
    //... 
    f.get(); // Without this, there is no guarantee that DoSomething() will ever be called. 
}

 

2. std::launch::async로 async()를 호출했을 경우는 해당 future object의 destructor가 해당 루틴이 끝날 때까지 block된다. 따라서 async() 자체가 실패하지 않았다면(실패의 경우 std::system_error를 throw) get()이나 wait()를 호출하지 않아도 해당 루틴이 호출됨이 보장된다.

 

3. async()로 여러 루틴을 concurrent하게 launch하려면...

 

  • std::launch::async 사용
  • async()가 리턴하는 future object를 반드시 assign할 것. 그렇지 않으면 단지 sequential call이 될 뿐임.

 

#include <future> 

void foo() 
{ 
    // Do something... 
} 

void bar() 
{ 
    // Do something... 
} 


int main() 
{ 
    try { 
        auto f1 = std::async(std::launch::async, []{ foo(); }); 
        auto f2 = std::async(std::launch::async, []{ bar(); }); 
        //... 
    } 
    catch (std::system_error & ) {
        // Failed to call asynchronously 
    } 
}

'programming > C++' 카테고리의 다른 글

Universal reference  (0) 2013.02.18
shared_ptr, unique_ptr  (0) 2013.02.18
constexpr  (0) 2013.02.18
Uniform initialization in C++11  (0) 2013.02.18
Three Optimization Tips for C++  (0) 2013.02.18
Posted by 무한자전거
,

constexpr

programming/C++ 2013. 2. 18. 06:11

다음과 같은 걸 할 수 있다.

 

 

1
2
3
4
5
6
constexpr int getDefaultArraySize (int multiplier)
{
    return 10 * multiplier;
}
 
int my_array[ getDefaultArraySize( 3 ) ];

 

제약 사항

 

  1. It must consist of a single return statement.(typedef, using declaration, using directive, static_assert()는 허용됨)
  2. It can call only other constexpr functions.
  3. It can reference only constexpr global variables.

 * Recursion은 허용됨

 

 

Example of constexpr function

 

 

1
2
3
4
constexpr factorial (int n)
{
    return n > 0 ? n * factorial( n - 1 ) : 1;
}

 

 

 

constexpr and run time

function argument가 non-constant일 경우, run time에 호출될 수 있다.

이는 compile time과 run time용의 별도의 function을 만들 필요가 없음을 말한다.

 

 

1
2
3
int n;
cin >> n;
factorial( n );

 

 

 

Using objects at compile time

 

다음과 같은 class가 있다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
class Circle
{
    public:
    Circle (int x, int y, int radius) : _x( x ), _y( y ), _radius( radius ) {}
    double getArea () const
    {
        return _radius * _radius * 3.1415926;
    }
    private:
        int _x;
        int _y;
        int _radius;
};

 

 

다음과 같이 compile time에 생성하고 면적을 얻으려면 어떻게 하면 되나?

 

 

1
2
constexpr Circle c( 0, 0, 10 );
constexpr double area = c.getArea();

 

 

constructor와 getArea()가 constexpr이어야 한다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
class Circle
{
    public:
    constexpr Circle (int x, int y, int radius) : _x( x ), _y( y ), _radius( radius ) {}
    constexpr double getArea ()
    {
        return _radius * _radius * 3.1415926;
    }
    private:
        int _x;
        int _y;
        int _radius;
};

 

 

constexpr constructor는 empty body를 가져야 하며, member initializer들 또한 compile time constant이어야 한다.

'programming > C++' 카테고리의 다른 글

Universal reference  (0) 2013.02.18
shared_ptr, unique_ptr  (0) 2013.02.18
std::async and std::future 쓸 때...  (0) 2013.02.18
Uniform initialization in C++11  (0) 2013.02.18
Three Optimization Tips for C++  (0) 2013.02.18
Posted by 무한자전거
,