26 Temmuz 2019 Cuma

extern

1. extern Specifier İle Global Değişken
extern Specifier İle Global Değişken yazısına taşıdım.

2. extern "C"
extern "C" yazısına taşıdım.

3. extern "C++"
Bu kullanımı hiç görmedim ancak anladığım kadarıyla şöyle yapılabiliyor.
extern "C++" char* helloWorld() {
  ...
}


inline specifier

Giriş
inline kelimesinin iki anlamı var.

1. Kodu yapıştırmak
Açıklaması şöyle.
...taking the code in the function and injecting it into the place where it is called.

The C++ standard advises implementations to do this when they see an inline method or function, but does not require it. As such action has zero observable behavior changes in the abstract machine that the C++ standard describes, I consider it non-normative advice.
1.1 Otomatik Inline
Açıklaması şöyle.
When a member function is defined inside a class definition, inline is implied.
Açıklaması şöyle.
A member function may be defined in its class definition, in which case it is an inline member function, or it may be defined outside of its class definition if it has already been declared but not defined in its class definition.
Örnek
Elimizde şöyle bir kod olsun.
#include <iostream>

class Greet
{
public:
    void greet()
    {
        std::cout << "Hello World!" << std::endl;
    }
};
A.cpp dosyasında şöyle yaparız.
#include <iostream>
#include "Greet.h"

int main()
{
    Greet G;
    G.greet();

    std::cin.get();
}
B.cpp dosyasında da şöyle yaparız.
#include "Greet.h"
Normalde şöyle bir hata almamız gerekirdi
fatal error LNK1169: one or more multiply defined symbols found.
Ancak metod otomatik inline yapıldığı için sorun çıkmaz.

2.Aynı Inline Metodun İki Farklı Yerde Vücut Bulması
Inline kod birden fazla yerde tanımlı ise aynı olması beklenir. Eğer farklılık varsa bir tanesi seçilir.
Açıklaması şöyle.
An inline function (or in C++17 a variable) can exist in multiple translation units. Normally this causes an error at link-time; but when the variable or function is inline, instead all but one of the instances of the variable or function are silently discarded. If they differ in any important way, this makes your program ill-formed no diagnostic required.

This second meaning is why implicit ctors and dtors are implicitly inline; it means that no single translation unit has to be chosen for them to "live in". Instead, they are generated everywhere they are needed. They may be preferentially actually inlined into calling code, but most importantly if any vestigial copies of it still exist (because it was not inlined, say), no error occurs at link time, and instead all but one of them are discarded.
Örnek 
İki tane inline foo metodu olsun
// file1.cpp
#include <iostream>


inline void foo()
{
  std::cout << "f1\n";
}

void f1()
{
  foo();
}
Diğer metod şöyle olsun
// file2.cpp
#include <iostream>


inline void foo()
{
  std::cout << "f2\n";
}

void f2()
{
  foo();
}
Bu metodları çağıralım.
void f1();
void f2();

int main()
{
    f1();
    f2();
}
Çıktı olarak şunu alırız.
f1
f1
Açıklaması şöyle
An inline function shall be defined in every translation unit in which it is odr-used and shall have exactly the same definition in every case (3.2). [...]
3. Inline Global Değişken
Örnek
Şöyle yaparız.
header.h dosyasında X sınıfı olsun. Bu dosyada inline X myX; değişkeni tanımlanır. Daha sonra A.cpp ve B.cpp dosyalarında header.h include edilir ve sorun çıkmaz. Bu kullanım extern kullanımı ile aynıdır.


24 Temmuz 2019 Çarşamba

std::any_of

Giriş
Şu satırı dahil ederiz
#include <algorithm> // std::any_of
Birinci imza şöyle.
template <class InputIterator, class Predicate>
bool any_of(InputIterator first, InputIterator last, Predicate pred);
İkinci imza şöyle.
template <class ExecutionPolicy, class ForwardIterator, class Predicate>
bool any_of(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last,
  Predicate pred);
Açıklaması şöyle. Muhtemelen short circuit yaparak ilk true veren elemanı görünce hemen döner.
Returns: false if [first, last) is empty or if there is no iterator i in the range [first, last) such that pred(*i) is true, and true otherwise.
Complexity: At most last - first applications of the predicate.
Örnek
Şöyle yaparız.
bool found = std::any_of(vector.begin(),
                         vector.begin(),
                         [](auto item) -> bool { return item == <xxx>; });
Örnek
Şöyle yaparız. Dizideki karakterlerden herhangi birisi ise isValid() true döner.
#include <array>     // std::array
#include <algorithm> // std::any_of

const std::array<char, 4> options{ '+', '-', '*', '/' };
const auto tester = [&temp](char c) { return temp->left->oper == c ||
                                             temp->right->oper == c; };
const bool isValid = std::any_of(options.cbegin(), options.cend(), tester);

while(isValid) // now the while-loop is simplified to
{
    // do something
}