Giriş
Açıklaması şöyle
Örnek
std::decay() std::remove_reference() çağrısından farklıdır. Şöyle yaparız.
std::decay olmadan şu çıktıyı alırız.
Açıklaması şöyle
Açıklaması şöyle. Yani const, volatile gibi qualifier'ları kaldırır.Simply put, decay::type is the identity type-transformation except if T is an array type or a reference to a function type. In those cases the decay::type yields a pointer or a pointer to a function, respectively.
referans olarak geçilen parametrenin sadece tipini almamızı sağlar.Applies lvalue-to-rvalue, array-to-pointer, and function-to-pointer implicit conversions to the type T, removes cv-qualifiers, and defines the resulting type as the member typedef type.
Örnek
std::decay() std::remove_reference() çağrısından farklıdır. Şöyle yaparız.
#include <type_traits>
int main()
{
    static_assert(std::is_same_v<
        std::decay_t<const int&>, 
        std::remove_reference_t<const int&>
    >); // int != const int
}std::decay olmadan şu çıktıyı alırız.
template<class T>
void func(T&& param) {
    if (std::is_same<T,int>::value) 
        std::cout << "param is an int\n";
    else 
        std::cout << "param is not an int\n";
}
int main() {
    int three = 3;
    func(three);  //prints "param is not an int"!!!!
}template<class T>
void func(T&& param) {
    if (std::is_same<typename std::decay<T>::type,int>::value) 
        std::cout << "param is an int\n";
    else 
        std::cout << "param is not an int\n";
} 
Hiç yorum yok:
Yorum Gönder