簡體   English   中英

奇怪的函數語法:auto(* f3)(int n) - > int(*)[n];

[英]Strange function syntax: auto (*f3)(int n)->int (*)[n];

我遇到了這種奇怪的函數語法:

const int n = 3;

auto (*f3)(int n)->int (*)[n]; //error: parameter 'n' as array bound

在cppreference.com上閱讀此頁面的范圍。

雖然第二個陳述是錯誤的,但你如何解釋呢? (假設范圍錯誤已得到糾正)

我認為第一部分是一個指向函數的指針,但它的部分來自 - >從那開始讓我感到難過。

有人能指出我正確的方向嗎? 謝謝

帶有auto和尾隨返回類型的->語法是C ++ 11中的新增功能。 你不能直接應用內而外的聲明解釋規則,整個事情,才單獨部件的左->和右->

如果我們擺脫錯誤

const int n = 3;
auto (*f3)(int m) -> int (*)[n];

那么適當的等效“經典”版本可以寫成

const int n = 3;
typedef int (*T)[n];

T (*f3)(int m);

int (*)[n]部分是返回類型。

換一種說法

T (*f3)(int m);

auto (*f3)(int m) -> T;

是一回事。 typedef有助於強調等價。

雖然第二個陳述是錯誤的,但你如何解釋呢? (假設范圍錯誤已得到糾正)

示例顯示了btw 2個案例的區別:

const int n = 3;

int (*(*f2)(int n))[n];

基本上相當於:

const int n = 3;

int (*(*f2)(int n1))[n];

const int n = 3;

auto (*f3)(int n)->int (*)[n];

相當於:

const int n = 3;

auto (*f3)(int n1)->int (*)[n1];

和文章說明了原因。 如果您的意思是修復此代碼:

const int n = 3;

auto (*f3)(int n1)->int (*)[n];

然后它會聲明一個指向函數的指針,該函數接受一個int類型的參數並返回指向3個int的數組的指針。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM