31 Temmuz 2017 Pazartesi

std:memmove

Giriş
std::memcpy yazısına göz atabilirsiniz.

source ve destination alanları çakışabilir. Açıklaması şöyle
The memmove() function copies n bytes from memory area src to memory area dest. The memory areas may overlap: copying takes place as though the bytes in src are first copied into a temporary array that does not overlap src or dest, and the bytes are then copied from the temporary array to dest.
İmzası şöyle
void *memmove (void *dest, const void *src, size_t n);
Return Value açıklaması şöyle
 The memmove function returns the value of dest
dest alanını döndüğü için şöyle yapabiliriz.
puts (memmove(dest, src, src_size));


Partial Template Specialziation

Giriş
Template için kullanılan bazı parametrelerin kod içinde belirlenmesi anlamına gelir. Template Specialization yazısına da göz atabilirsiniz.

Örnek - genişleme
Önce primary template yazılır. Şöyle yaparız.
template<typename T, int... N>
struct lookup_table_expand {
};
Sonra genişleyen kod yazılır. Şöyle yaparız. Burada kalıtım da kullanılıyor.
template<typename T, int L, int... N> 
struct lookup_table_expand<T, L, N...> :
  lookup_table_expand<T, L - 1, look_up_table_elem<T>(L - 1), N...> {
};
Daha sonra partial template specialization ile genişleme durdurulur. Genişleme L değeri 1 olunca durur. Şöyle yaparız.
template<typename T, int... N>
struct lookup_table_expand<T, 1, N...> {
  static constexpr std::array<T, sizeof...(N) + 1> values = {{
        look_up_table_elem<T>(0), N...
    }};
};
values değişkenine değer atamak için şöyle yaparız.
constexpr uint16_t look_up_table_elem(int i) {
    return ...;
}
Bu değişken static olduğu için bir tabloya doldurmak için şöyle yaparız.
const std::array<uint16_t, 2048> lookup_table =
  lookup_table_expand<uint16_t, 2048>::values;

27 Temmuz 2017 Perşembe

move constructible traits

Sınıfın Copyable Olması Yeterli
Açıklaması şöyle
A class copyable is still MoveConstructible and MoveAssignable
Bir diğer açıklama şöyle
Moving can be seen as a special case where the original variable need not be preserved. Preserving the original variable (like copying) does not invalidate the move.
Şöyle yaparız.
struct copyable { // and movable
  copyable() = default;
  copyable(copyable const&) { /*...*/ };
  copyable& operator=(copyable const&) { /*...*/ return *this; }
};
std::is_move_constructible metodu
Açıklaması şöyle
A defaulted move constructor that is defined as deleted is ignored by overload resolution.
Elimizde kalıtım yapan iki sınıf olsun. Foo halen move_constructable olarak true döner.
template <typename Base> struct Foo : public Base {
  using Base::Base;
};

struct Bar {
  Bar(const Bar&) { }
  Bar(Bar&&) = delete;
};

std::cout << std::is_move_constructible<Bar>::value << std::endl; // NO
std::cout << std::is_move_constructible<Foo<Bar>>::value << std::endl; // YES.

24 Temmuz 2017 Pazartesi

Variadic Macro

__VA_ARGS__
Bu macro bir GNU extension. Bir metoda istediğimiz kadar parametre geçmek için şöyle yaparız.
#define chkErr(FUNCTION, ...)  \
    if(!FUNCTION(__VA_ARGS__)) \
    {                          \
        perror(#FUNCTION);     \
    }
Ancak bu kullanım hatalara sebep olabiliyor. Şöyle yapmak daha iyi.
#define chkErr(FUNCTION, ARGUMENTS) \
do                                  \
{                                   \
    if(!FUNCTION ARGUMENTS)         \
    {                               \
        perror(#FUNCTION);          \
    }                               \
}                                   \
while(0)

chkErr(someFunction,(12, 10));
//                 ^ (!)