数组类型的退化Decay

Decay即数组在某些情况下将退化为指针。

测试代码:

#include <iostream>
#include <typeinfo>

template <typename T>
void ref (T const& x)
{
    std::cout <<"x in ref(T const&): " << typeid(x).name() << std::endl;
}

template <typename T>
void nonref(T x)
{
    std::cout << "x in nonref(T): " << typeid(x).name() << std::endl;
}

void func(char a[20], size_t b)
{
    std::cout << b << " " << sizeof(a) << std::endl;
}

int main()
{
    ref("hello");
    nonref("hello");

    char c[20] = "Hello World!";
    func(c, sizeof(c));
    return 0;
}

注意结果中的类型信息。

结果:

原文地址:https://www.cnblogs.com/AmitX-moten/p/4452229.html