8 Eylül 2021 Çarşamba

std::isprint metodu

Giriş
Şu satırı dahil ederiz
#include <cctype>
Örnek
Şöyle yaparız.
char c = ...;
if (isprint(c)) {
  ...
}

std::isspace metodu

Giriş
Şu satırı dahil ederiz
#include <cctype>
Örnek
Şöyle yaparız.
char c = ...;
if (isspace(c)) {
...
}

10 Ağustos 2021 Salı

Deleted Function

Giriş
Deleted Constructor
Deleted Destructor
olabilir.
Ayrıca deleted generic function da olabilir.

Örnek
Şöyle yaparız
// API function
void api(void* p) {
  // ... 
}

// Special case for nullptr
inline void api(std::nullptr_t) {
  api((void*)nullptr);
}

// Everything else is disabled
template <class ...T>
void api(T&&... t) = delete;

int main() {
  int i = 0;
  void* p = &i;
  api(p); // Compiles
  // api(&i); // Doesn't compile
}

std::views::common metodu

Giriş
std::ranges::range tipinden olan değişkeni, STL Container gibi kullabilmeyi sağlar.
Örnek
Şöyle yaparız
template <std::ranges::range R>
auto to_vector(R&& r) {
    auto r_common = r | std::views::common;
    return std::vector(r_common.begin(), r_common.end());
}
Örnek
Şöyle yaparız
#include <cmath>
#include <numeric>
#include <ranges>
#include <utility>

template<typename Container>
auto mean_and_stddev(const Container& values)
{
  auto len = static_cast<double>(values.size());
  auto mean = std::accumulate(values.begin(), values.end(), 0.0) / len;
  auto sdv =                   // squared differences view
      values
      | std::views::transform([mean](auto d){ auto x = d - mean; return x*x; })
      | std::views::common;
  auto stddev = std::sqrt(std::accumulate(sdv.begin(), sdv.end(), 0.0) / len);
  return std::pair{ mean, stddev };
}

1 Temmuz 2021 Perşembe

C++ Filename Extensions

Giriş
C++ ve bir çok daha başka dil (Pascal, BASIC, C gibi) dosya uzantısının ne olması gerektiğini belirtmez. Bjarne Stroustrup'un "The C++ Programming Language (2nd edition, 1991)" kitabındaki Ch.4, p114'teki açıklaması şöyle açıklaması şöyle
Header files are conventionally suffixed by .h and files containing function or data definitions by .c. ... Other conventions, such as .C, .cxx, .cpp, and .cc, are also found. The manual for your compiler will be quite specific about this issue.

21 Haziran 2021 Pazartesi

Name Mangling - Sadece C++'ta Vardır

Giriş
Açıklaması şöyle. Yani Linker için gereklidir.
Name mangling is the encoding of function and variable names into unique names so that linkers can separate common names in the language. 
Eğer C kütüphanesini C++ ile kullanmak istersek name mangling olmasını istemeyiz. Bu durumda extern "C" kullanmak gerekir.

Örnek
Açıklaması şöyle
If you have ever looked at an objdump of a C++ program, you have likely seen something like this:

_ZN3foo3bar3bazI6sampleEE3quxvi3fo

This is a C++ mangled symbol, which encodes the namespaces, classes, and function/template arguments, using the Itanium ABI.

Specifically, it is for the following function:

void foo::bar::baz<sample>::qux(int, foo);
Rakamların açıklaması şöyle
- All mangled symbols start with _Z.
- Everything is case sensitive.
- All identifiers are encoded as <length><name>, where <length> is the positive length of <name> in base 10, so foo is encoded as 3foo, sample is encoded as 6sample, etc.

7 Haziran 2021 Pazartesi

std::chrono::year_month_day

Örnek
Şöyle yaparız. Bu kod UTC saate göre çalışır
#include <chrono>

bool IsTodayChristmas() {
    using namespace std::chrono;

    constexpr month_day Christmas = {December / 25};
    auto Now = year_month_day{floor<days>(system_clock::now())};

    // either
    return Now == Christmas / Now.year();
    // or
    return Now.month() / Now.day() == Christmas;
}
Yerel zamana göre çalışmak istersek şöyle yaparız
bool IsTodayChristmas() {
    using namespace std::chrono;

    constexpr month_day Christmas = {December / 25};
    auto Now_local = current_zone()->to_local(system_clock::now());
    auto Today = year_month_day{floor<days>(Now_local)};

    return Today == Christmas / Today.year();
}