3 Şubat 2020 Pazartesi

std::signed_integral

Giriş
Kavramsal olarak tanımı şöyle.
template<typename T>
concept integral = std::is_integral_v<T>;

template<typename T>
concept signed_integral = std::is_integral_v<T> && std::is_signed_v<T>;
Açıklaması şöyle.
In this example, signed_integral implies integral completely. So in a sense, a signed integral is "more constrained" than an integral.
Örnek
Elimizde şöyle bir kod olsun. std::signed_integral tercih edilir.
#include <concepts>
#include <iostream>

template<class T>
struct wrapper;

template<std::signed_integral T>
struct wrapper<T>
{
  wrapper() = default;
  void print()
  {
    std::cout << "signed_integral" << std::endl;
  }
};

template<std::integral T>
struct wrapper<T>
{
  wrapper() = default;
  void print()
  {
    std::cout << "integral" << std::endl;
  }
};

int main()
{
  wrapper<int> w;
  w.print(); // Output : signed_integral
  return 0;
}

Hiç yorum yok:

Yorum Gönder