From c13fedbb1344f40765580922718ec9f95d20f079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=B5=E4=B8=8D=E4=BD=8F=E7=9A=84=E6=97=B6=E9=97=B4?= Date: Thu, 13 Jul 2023 06:04:55 +0000 Subject: [PATCH] =?UTF-8?q?=E7=A5=9E=E5=A5=87=E5=BC=95=E7=94=A8=E5=9C=A8?= =?UTF-8?q?=E5=93=AA=E9=87=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 抵不住的时间 --- C++/RetLocalParamPlus.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 C++/RetLocalParamPlus.cpp diff --git a/C++/RetLocalParamPlus.cpp b/C++/RetLocalParamPlus.cpp new file mode 100644 index 0000000..5304efb --- /dev/null +++ b/C++/RetLocalParamPlus.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +/* + * 根据 decltype(i) 推断类型,返回类型为 int + */ +decltype(auto) +fn_1(int i) { + return i; +} + +/* + * 根据 decltype((i)) 推断类型,由于(i)是左值,返回类型为 int& + * 从而产生局部变量的引用 + */ +decltype(auto) +fn_2(int i) { + return (i); +} + +int main() { + cout << fn_1(1) << endl ; // OK + cout << fn_2(1) << endl ; // Segmentation fault +} -- Gitee