11 Ekim 2018 Perşembe

C++17 ile If Statement - If statement with initializer

Giriş
Gramer şöyle.
selection-statement:  
    if ( init-statement condition )

init-statement:
    simple-declaration

simple-declaration:
    decl-specifier-seq init-declarator-list;
    decl-specifier-seq ref-qualifier [ identifier-list ] initializer ;
Yapısı şöyle. Bu kullanımın tüm amacı if işleminin sonucunu alan değişkenin scope alanını küçültmek
if (init-statement; condition) statement
Örnek
Eskiden şöyle yapardık. result değişkeni if bloğunun dışında yaşamaya devam ederdi.
bool result = foo();
 if( result ) {
    std::cout << "True!\n";
 } else {
    std::cout << "False!\n";
 }
Şimdi şöyle yaparız.
if( bool result = foo(); result ) {
    std::cout << "True!\n";
 } else {
    std::cout << "False!\n";
 }
Örnek
Şöyle yaparız. r false ise do doSomething() çalışır.
if (auto r = getGlobalObjectByName(word); !r) r->doSomething;
Örnek
Biraz daha karışık kodlar ortaya çıkabiliyor. Şöyle yaparız
int bar();
bool query();
void foo(int, bool);

int main()
{
  if (int x = bar() ; bool y = query())
  {
    foo(x, y);
  }
  else
  {
    foo(x * 2, y);
  }
}

If Statement ve Structured Binding
Açıklaması şöyle.
A simple-declaration is either a comma separated list of declarators that introduce objects of the same type (decl-specifier-seq init-declarator-list;) or a single structured binding (the second, rather verbose line under simple-declaration).
Örnek
Şöyle yaparız.
std::map<int, std::string> Map;
// ...

if (auto[it, inserted] = Map.insert(std::pair(10, "10")); inserted)
    ; // do something with *it.
Örnek
Şu kod birden fazla "structured binding" kullandığı için derlenmez
if (auto[iter, success] = set.insert("Hello"),
        [iter2, success2] = set.insert("Foo"); success && success2)
{}
else
{}
Şöyle yaparız
if (auto[iter, success, iter2,success2] =std::tuple_cat (set.insert("Hello"),
        set.insert("Foo")); success && success2)
{}
else
{}

1 Ekim 2018 Pazartesi

fflush metodu

Giriş
Şu satırı dahil ederiz.
#include <stdio.h>
Açıklaması şöyle.
int fflush(FILE *ostream);
ostream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.
Örnek
Elimizde şöyle bir kod olsun
int main(int argc, char ** argv) {
  printf("this is a test message.\n");
  system("ls");

  return 0;
}
Bunu şöyle çalıştıralım. Çıktının karışık sırada geldiğini görebiliriz.
myprogram > output.txt
Çıktı olarak şunu alırız.
$ ./main > foo
$ cat foo
main
main.c
this is a test message.
Araya fflush koyalım.
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char ** argv) {
  printf("this is a test message.\n");
  fflush(stdout);
  system("ls");

  return 0;
}
Çıktı olarak şunu alırız.
$ ./main > foo
$ cat foo
this is a test message.
foo
main
main.c
Output Stream'i Olmayan Kodlar
Şu tarz kodlar doğru değil. Çünkü stdin FILE* olmasınra rağmen output stream değil.
fflush(stdin);