template<typename T>
void print_size(const T& x)
{
std::cout << sizeof(x) << '\n';
}
int main()
{
print_size("If you timidly approach C++ as just a better C or as an object-oriented language, you are going to miss the point.");
// prints 115
}
这会在最近的 g++ 编译器上打印 115。所以很明显,T
被推断为一个数组(而不是一个指针)。标准是否保证了这种行为?我有点惊讶,因为下面的代码打印了指针的大小,我认为它的auto
行为与模板参数推导完全一样?
int main()
{
auto x = "If you timidly approach C++ as just a better C or as an object-oriented language, you are going to miss the point.";
print_size(x);
// prints 4
}