20 Eylül 2016 Salı

std::isnan

Giriş
Şu satırı dahil ederiz.
#include <cmath>
C++11 ile geldi.

isnan - double
Şöyle yaparız.
double val = ...;
if (std::isnan (val)) {...}
isnan - float
Örnek ver

isnan - long double
Örnek ver

std::isfinite

Giriş
Şu satırı dahil ederiz.
#include <cmath>
C++11 ile geldi.

isfinite - double
Şöyle yaparız.
double val = value;
if (std::isfinite(val)) {...}
isfinite - float
Örnek ver

isfinite - long double
Örnek ver

15 Eylül 2016 Perşembe

ForwardIterator

Giriş
5 tane iterator kategorisi var. Bunlar
1. input_iterator_tag - InputIterator yazısına bakınız.
2. output_iterator_tag - Output Iterator yazısına bakınız.
3. forward_iterator_tag - Bu yazı
4. bidirectional_iterator_tag,
5. random_access_iterator_tag - RandomAccessIterator yazısına bakınız.

forward_iterator_tag,bidirectional_iterator_tag ve random_access_iterator_tag yapıları aynı zamanda output_iterator_tag'den kalıtır.

Açıklaması şöyle
Forward iterators have the "Multipass guarantee", allowing repeated dereference of copies of iterators, yielding the same result.
Tanımlama
Şöyle yaparız.
struct iterator : std::iterator<std::forward_iterator_tag, T> {...}
operator * metodu
İmzası şöyledir
T& operator*() const;
operator ++ metodu
İmzası şöyledir
iterator& operator++() {
  ...
  return *this;
}
operator == metodu
İmzası şöyledir
bool operator==(const iterator& r) const;
operator != metodu 
İmzası şöyledir
bool operator!=(const iterator& r) const;


12 Eylül 2016 Pazartesi

Comma Operator

Comma Operator ve Assignment Operator
Comma operator daha düşük önceliğe sahiptir. Elimizde şöyle bir kod olsun
#include <iostream>
int main() {
  int x;
  x = 2,3;
  std::cout << x << "\n"; //2
  return 0;
}
Çıktı olarak 2 alırız çünkü kod şöyledir.
> x = 2
> 3
Comma Operator ve return
Elimizde şöyle bir kod olsun
#include <iostream>
int f() { return 2,3; }
int main() {
  int x;
  x = f();
  std::cout << x << "\n"; //3
  return 0;
}
Çıktı olarak 3 alırız çünkü kod şöyledir.
> 2
> 3


11 Eylül 2016 Pazar

Statement Order

Giriş
Açıklaması şöyle. Niyeyse bu konuda tam bir uyuşma sağlanamamış.
14 Every value computation and side effect associated with a full-expression is sequenced before every value computation and side effect associated with the next full-expression to be evaluated.
Elimizde şöyle bir kod olsun.
using Clock = std::chrono::high_resolution_clock;

auto t1 = Clock::now(); // Statement 1
foo();                  // Statement 2
auto t2 = Clock::now(); // Statement 3

auto elapsedTime = t2 - t1;
Bu kodun sırasının değişmemesi gerekir. Eğer şu hale gelirse kod bozulur
using Clock=std::chrono::high_resolution_clock;

foo();                  // Statement 2
auto t1 = Clock::now(); // Statement 1
auto t2 = Clock::now(); // Statement 3

auto elapsedTime = t2 - t1;
as-if kuralı
Kod şöyle olsun
int fred(int x)
{
  auto t1 = std::chrono::high_resolution_clock::now();
  int y = foo(x);
  auto t2 = std::chrono::high_resolution_clock::now();
  return y;
}
t1 ve t2 değişkenleri kullanılmıyor. Bu durumda kod içindeki now() metodları teorik olarak yer değiştirebiliyor.

Buna as-if kuralı deniyor. Yani gözlemlenebilen davranışta değişiklik olmaması. Tabi bu kuralı uygulamak ve ispatlamak derleyici açısında zor. Özellikle sistem çağrılarının yan etkisinin olmadığını bilmesi imkansız gibi.






10 Eylül 2016 Cumartesi

std::rethrow_exception metodu

Şöyle yaparız.
std::exception_ptr ex = ...;
if (ex) {
  std::rethrow_exception (ex);
}

7 Eylül 2016 Çarşamba

Gcc Labels As Values

Giriş
Gcc Labels As Values veya Computed gotos olarak bilir. Gcc'nin sağladığı ek bir özelliktir.

Tanımlama
Computed gotos şöyle tanımlanır.
static void *arr[1]  = {&& varOne,&& varTwo,&& varThree};

varOne: printf("One") ; goto *VarTwo;varTwo: printf("Two") ; goto *VarThree;varThree: printf("Three");
State Machine
Şöyle yaparız.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    void *tab[] = { &&foo, &&bar, &&qux };

    // Alternative method
    //int otab[] = { &&foo - &&foo, &&bar - &&foo, &&qux - &&foo };

    int i, state = 0;

    srand(time(NULL));

    for (i = 0; i < 10; ++i)
    {
        goto *tab[state];

        //goto *(&&foo + otab[state]);

    foo:
        printf("Foo\n");
        state = 2;
        continue;
    bar:
        printf("Bar\n");
        state = 0;
        continue;
    qux:
        printf("Qux\n");
        state = rand() % 3;
        continue;
    }
}
Çıktı olarak şunu alırız.
$ gcc -o x x.c && ./x
Foo
Qux
Foo
Qux
Bar
Foo
Qux
Qux
Bar
Foo

5 Eylül 2016 Pazartesi

new_handler

Giriş
new_handler function pointer olarak tanımlıdır ve şöyledir.
typedef void (*new_handler)();
Bellek ayırırken hata olursa çağrılır. Kod şuna benzer
// The allocation was unsuccessful; find out what the
// current new-handling function is 
new_handler globalHandler = set_new_handler(0);
set_new_handler(globalHandler);
if (globalHandler)
  (*globalHandler)();
else
  throw std::bad_alloc();
C++11
new_handler'ı almak için şöyle yaparız.
new_handler globalHandler = get_new_handler();
C++98
new_handler'ı almak için şöyle yaparız.
new_handler globalHandler = set_new_handler(0);
set_new_handler (globalHandler);

Preprocessor

#define
Şöyle yaparız.
#define A 2
#if
Şöyle yaparız.
#define A 2
#define B 5
#if (A && B)
  printf("A && B\n");
#endif

return Anahtar Kelimesi

Metod Sonucu
Bir sonuç dönmesi beklenen metod, dönmez ise tanımsız davranışa sebep olur. Açıklaması şöyle
Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function C++11 Draft pg 136
Şu kod tanımsız davranış gösterirr
bool MyClass::Foo () {
  do some stuff and forget the return value
}

3 Eylül 2016 Cumartesi

perror

Giriş
Şu satırı dahil ederiz.
#include <stdlib.h>
Genellikle uygulama ismi de yazdırılır.
int main(int argc, char *argv[])
{
    char *foo = malloc(9999999999L);
    if (foo == 0)
        perror(argv[0]);
    return 0;
}
Çıktı olarak şunu alırız.
> ./foo
./foo: Cannot allocate memory