gpt4 book ai didi

c++ - C++11 类型推导中的右值引用

转载 作者:行者123 更新时间:2023-11-30 01:51:00 28 4
gpt4 key购买 nike

<分区>

在C++11之前,模板类型推导非常简单:

template <typename X>
void bar(X i) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}

类型 X 将是调用者传入的任何类型的参数。

现在,对于 C++11,我从 Scott Meyers 阅读了一篇关于右值引用的文章.

它说

template<typename T>
void f(T&& param);

During type deduction for a template parameter that is a universal reference, lvalues and rvalues of the same type are deduced to have slightly different types. In particular, lvalues of type T are deduced to be of type T& (i.e., lvalue reference to T), while rvalues of type T are deduced to be simply of type T.

我想知道为什么类型 T 的左值被推导为 T& 类型,而类型 T 的右值被推导为简单的 T 类型。

仅仅是为了内存吗?或者?

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com