11 Eylül 2017 Pazartesi

std::aligned_storage

Giriş
Sanırım en çok placement new ile kullanır.

Tanımlama
Şöyle yaparız.
typename std::aligned_storage<sizeof(T) + 1, alignof(T)>::type bytes_{};
Örnek
Elimizde move işlemini destekleyen bir yapı olsun.
struct moveonly {
  int x;
  moveonly(int x) noexcept : x(x) {}
  moveonly(moveonly&& o) noexcept : x(o.x) { o = {-1}; }
  moveonly& operator=(moveonly o) noexcept {using std::swap; swap(x, o.x); return *this;}
};
Bir bellek alanı oluştururuz.
std::aligned_storage<sizeof(moveonly), alignof(moveonly)>::type buffer;
moveonly* extracted = reinterpret_cast<moveonly*>(&buffer);
Elimizde bir iterator olsun. Şöyle yaparız.
std::aligned_storage<sizeof(moveonly), alignof(moveonly)>::type buffer;
moveonly* extracted = reinterpret_cast<moveonly*>(&buffer);

auto it = ...;
new (extracted) moveonly{std::move(*it)}; 


8 Eylül 2017 Cuma

std::bind2nd

Giriş
İmzası şöyle
template< class F, class T >
std::binder2nd<F> bind2nd (const F& f, const T& x);
binder ise şöyledir
std::binder2nd<F>(f, typename F::second_argument_type(x))
Lambda çıktıktan sonra bu algoritmalara gerek kalmadı. Bir sonuç dönen bir metod üretir.

Örnek - global metod
Şöyle yaparız.
vector<int> v1; 
// Count the number of integers > 10 in the vector 
count_if (v1.begin(), v1.end(), bind2nd( greater<int>(), 10 ) ); 
Örnek - mem_fun
Şöyle yaparız.
struct Foo
{
  bool comp(const Foo& a)
  {
    ...
  }

};

template <class F, class T>
void execute (F f, T a)
{
  std::cout << f (a) << std::endl;
}

Foo a = ...;

execute (std::bind2nd (std::mem_fun(&Foo::comp), b), f1);

7 Eylül 2017 Perşembe

strcoll

Giriş
strcmp() karakter karakter karşılaştırma yapar. strcoll() ise locale kurallarına dikkat ederek karşılaştırma yapar.

Örnek
Şöyle yaparız.
char array[50] = ...;
char array2[50] = ...;

if ( strcoll (array,array2) > 0 )  
{
  ...
}