26 Mart 2018 Pazartesi

_Generic - C11

Giriş 
C11 ile geliyor

Örnek
Elimizde şöyle bir macro olsun.
#define ICE_P(x) _Generic((1? (void *) ((x)*0) : (int *) 0), int*: 1, void*: 0)
Çağırmak için şöyle yaparız. Çıktı olarak 1 ve 0 alırız
printf("%d %d\n", ICE_P(1), ICE_P(x));
Örnek
Elimizde şöyle bir metod olsun
int test(char * a, char * b, char * c, bool d, int e);
Metodun imzasına göre 1 veya 2 değerini dönmek için şöyle yaparız. Çıktı olarak 1 alırız
#define WEIRD_LIB_FUNC_TYPE(T) _Generic(&(T), \
    int (*)(char *, char *, char *, bool, int): 1, \
    int (*)(char *, char *, char *, bool): 2, \
    default: 0)

printf("test's signature: %d\n", WEIRD_LIB_FUNC_TYPE(test));
// will print 1 if 'test' expects the extra argument, or 2 otherwise

23 Mart 2018 Cuma

std::any_cast

std::bad_any_cast Exception
Açıklaması şöyle.
Throws std::bad_any_cast if the typeid of the requested ValueType does not match that of the contents of operand.
Örnek
Bu exception'ı görmek için şöyle yaparız.
std::any a = 10;  // holds an int now
auto b = std::any_cast<long>(a);   // throws bad_any_cast exception


19 Mart 2018 Pazartesi

std::chrono::duration_cast metodu

chrono::duration_cast
duration_cast farklı duration tipleri arasında dönüşüm yapmak için kullanılır.
Örnek
Şöyle yaparız
typedef std::chrono::high_resolution_clock my_clock;

my_clock::time_point start = my_clock::now();

// Do stuff

my_clock::time_point end = my_clock::now();

std::chrono::milliseconds ms_duration = 
    std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
Örnek
Çevrimde kayıp olmasın yani gerekirse küsuratlı olsun istersek float kullanarak şöyle yaparız.
std::chrono::high_resolution_clock::duration d = ...;
if (d < std::chrono::microseconds(10))
cout
<< std::chrono::duration_castt<std::chrono::duration<float, std::nano>>(d).count()
<< " nnano s";
Örnek
std::duration_cast kodları uzun olduğu için şöyle yaparız.
template <typename Duration>
auto as_ms(Duration const& duration) {
    return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
}

void foo() {
  Time start = Clock::now();
  // something
  Time end = Clock::now();
  std::cout << as_ms(end - start).count() << std::endl;
}
C++17 ile şöyle yaparız.
using Clock = std::chrono::system_clock;
using Time = Clock::time_point;

auto Ms = [](auto&& duration)
{
  return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
};

auto start = Clock::now();
// something
auto end = Clock::now();
std::cout << Ms(end - start) << std::endl;




std::chrono duration Sınıfı - Zaman Aralığını Temsil Eder

1. Clock ile İlgili Sınıflar
Tip olarak şöyledir.
std::chrono::high_resolution_clock::duration
İki time timepoint'in farkı ile bulunur. Şöyle yaparız
auto begin = std::chrono::high_resolution_clock::now();
...
auto end = std::chrono::high_resolution_clock::now();
std::chrono::high_resolution_clock::duration diff = end - begin;
Diğer hazır sınıflar ile karşılaştırmak için şöyle yaparız.
if (diff < std::chrono::microseconds(10))      {...}
else if (diff < std::chrono::milliseconds(10)) {...}
else if (diff < std::chrono::seconds(10))      {...}
else if (diff < std::chrono::minutes(10))      {...}
2. Hazır Sınıflar
Hazır Sınıflar yazısına taşıdım.

Duration ve String
Chrono ile en problemli nokta duration nesnesini string'e çevrilmesidir. count metodu kullanılarak yapılır.