From: Joe Smith on 25 Dec 2009 10:08 It is well established that "int (*) (int)" is a pointer to a function that takes an integer and returns an int. But what about spelling the type of a function that takes and integer and returns an int. By first though was perhaps "int ()(int)", but Comeau C++ interpets that as a function that takes an int and returns a function that takes nothing and returns an int. It interpets "int (int)" as a function that takes an int, and returns an int. Is that correct? The GNU demangler appears to returns "int ()(int)" for this type, at least with the version I use, so one of the two must be wrong. Which is right, or are both wrong? I realize that there is very little use for such a spelling, since when declaring a function or typedef it needs a name, and in just about all other cases, it will decay to a function pointer, so one could always use the function pointer version spelling in those cases, but there should still be a spelling for this type. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Johannes Schaub (litb) on 26 Dec 2009 01:27 Joe Smith wrote: > It is well established that "int (*) (int)" is a pointer to a function > that takes an integer and returns an int. > > But what about spelling the type of a function that takes and integer and > returns an int. By first though was perhaps "int ()(int)", but Comeau C++ > interpets that as a function that takes an int and returns a function that > takes nothing and returns an int. > > It interpets "int (int)" as a function that takes an int, and returns an > int. > Is that correct? The GNU demangler appears to returns "int ()(int)" for > this type, at least with the version I use, so one of the two must be > wrong. Which is right, or are both wrong? > The demangler has a bug, and comeau c++ is right. The correct type is "int(int)". And for making up the function pointer type, you can do "identity<int(int)>::type*" which is somewhat easier and denotes the same type as "int(*)(int)". The parenthesis around * is for precedence. If you remove the pointer declarator, you need to remove that precedence paren, because otherwise the declarators have a different parse and meaning. > > I realize that there is very little use for such a spelling, since when > declaring a function or typedef it needs a name, and in just about all > other cases, it will decay to a function pointer, so one could always use > the > function pointer version spelling in those cases, but there should still > be a spelling for this type. > Yes when you write "void f();" you implicitly use that type for the name "f". Equivalently, you can put it into the decl-specifier-seq as "identity<void()>::type f;" . GCC doesn't always get it right, failing on this one: struct A { void f(); }; struct B { friend identity<void()>::type A::f; }; It thinks that you cannot declare a function without a function declarator here, but surely you can :) -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Stuart Golodetz on 26 Dec 2009 01:29 On 26/12/2009 03:08, Joe Smith wrote: > It is well established that "int (*) (int)" is a pointer to a function that > takes an integer and returns an int. > > But what about spelling the type of a function that takes and integer and > returns an int. By first though was perhaps "int ()(int)", but Comeau C++ > interpets that as a function that takes an int and returns a function that > takes nothing and returns an int. > > It interpets "int (int)" as a function that takes an int, and returns an > int. > Is that correct? The GNU demangler appears to returns "int ()(int)" for > this > type, at least with the version I use, so one of the two must be wrong. > Which is right, or are both wrong? > > > I realize that there is very little use for such a spelling, since when > declaring a function or typedef it needs a name, and in just about all > other > cases, it will decay to a function pointer, so one could always use the > function pointer version spelling in those cases, but there should still be > a spelling for this type. It's "int (int)", unless the Christmas festivities have dulled my mind :-) Sounds like the GNU demangler is confused. There are uses for it as well - for instance, in one of my wrapper libraries I have code like this: template <typename R, typename Arg0> struct ASXTypeString<R (Arg0)> { std::string name; explicit ASXTypeString(const std::string& name_) : name(name_) {} std::string operator()() { std::ostringstream os; os << ASXTypeString<R>()() << ' ' << name << '('; os << ASXTypeString<Arg0>().as_param()(); os << ')'; return os.str(); } }; Regards, Stu -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Nikolay Ivchenkov on 26 Dec 2009 01:23 On 26 Dec, 06:08, "Joe Smith" <unknown_kev_...(a)hotmail.com> wrote: > But what about spelling the type of a function that takes and integer and > returns an int. By first though was perhaps "int ()(int)", but Comeau C++ > interpets that as a function that takes an int and returns a function that > takes nothing and returns an int. > > It interpets "int (int)" as a function that takes an int, and returns an > int. > Is that correct? Yes. See 8.1 Type names [dcl.name] In the case of "int (int)": 1. left "int" matches type-specifier-seq 2. "(int)" matches abstract-declarator that has the form ( parameter-declaration-clause ) The type designated by "int (int)" is the same as the type of declarator-id "f" in the following declaration: int f(int); where abstract-declarator "(int)" is replaced by declarator "f(int)". -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Pages: 1 Prev: U++ 1824 released Next: Iterating over vectors - speed difference |