int ( *t(int) )( int* );
-
Hi,
habe diese deklaration auf http://www.gotw.ca/gotw/046.htm gesehen.
Herb Sutter:
...this is a function declaration, for a function named "t" that takes an int and returns a pointer to a function that takes an int* and returns an int.
Kann das nicht nachvollziehen. wäre das nichtint (*)(int*) t(int)?
rekursive deklaration? ein beispiel für c++'s vexing parsing?
Der VS compiler parst es zu
int(*(*t)(int*))(int)Hat jemand gedanken dazu? danke.
-
Klammern Setzung beachten
int ()(int) t(int)
Hier hast du keine sinnvollen Klammern gesetzt, ergo ist es ein syntax fehler.
Du kannst nicht einfach alles aneinanderreihen
das klappt so nicht. Zerlege das Konstrukt einfach:int ( (int) )( int );
nehmen wir die innerste Klammer:
(int)
ok, das sagt uns noch nicht viel, gehen wir einen schritt weiter:
*(int)
aha, ein zeiger auf eine funktion die einen int erwartet, gehen wir nun weiter:int ((int))(int)
ok, das ist jetzt kompliziert - tun wir mal so als wuerde die funktion nur einen int returnen und keinen funktionszeiger:int ((int))
ah, schoener.
wir sehen nun dass alles um unseren zeiger herum der return typ ist, ergo ist der return typ:
int ()(int*)ein Funktionszeiger der einen int* nimmt und einen int returned.
Das "dumme" an funktionszeigern ist, dass sie beide seiten vom typennamen mit informationen fuellen.
ein "int i;" ist ja simpel: alle informationen ueber i stehen links davon.
bei "int f(int);" stehen aber links und rechts informationen.und genau darum geht es in dem gotw ja: typedefs sind praktische helfer.
-
Vielen Dank.

Man könnte auch argumentieren, da es schlußendlich eine funktions(nicht funktionspointer)definition ist, daß auch (int ()(int)) t(int) valide sein sollte, zumal dies mit typedef funtioniert.
P.S. Wie würde eine
...function declaration, for a function named "t" that takes an int and returns a pointer to a function that takes an int* and returns a(n) (not int, but) pointer to a function that takes an int* and returns an int...
aussehen? int ((int (*u(int))(int*)))(int) geht nicht. nicht verschachtelbar?

-
new benutzer; schrieb:
...function declaration, for a function named "t" that takes an int and returns a pointer to a function that takes an int* and returns a(n) (not int, but) pointer to a function that takes an int* and returns an int...
aussehen? int ((int (*u(int))(int*)))(int) geht nicht. nicht verschachtelbar?

int (*(*t(int))(int*))(int*)
-
ah ja. sorry. bei VS leider
error C2091: Funktionsergebnis ist eine Funktion
-
gcc schluckt das. Falsch abgeschrieben, irgendwo einen Stern vergessen?
-
Stern vergessen.
Danke.