This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Unnamed functions, functors or, more formally, function literals


[trimmed to gcc@]

Paul V. Andreev wrote:

> I believe this is a feature most C programmers dream of (not
> mentioning many friends of mine and me myself). This one has already
> been proposed by several people (including the man that implemented
> nested functions in GCC compiler and Paul Long with his proposal for
> C language standardisation comitee).

I for one don't see why. I don't think it aids readability; to take your
example, except rename the function "compare" to "compare_strings", then
I consider:

  qsort(a,b,c, compare_strings);

a lot more readable than your:

  qsort(a,b,c, (int (void *a1, void *a2)
                {return strcmp(*(char **)a1, *(char)**)a2);}));

and a separate static function doesn't hurt much.

You may do better proposing it as a C++ extension, but I think you'll
lose out there too; the syntax doesn't look clean from a typing point of
view.

But then I didn't know GCC supported nested functions and I'm not sure I
like the idea of them much either.

Your driver example; if you're writing multiple driver structures,
though, you may want to share functions between them. I'd consider it
more obvious to both programmer and compiler to have:

    struct driver
    {
        void foo();
        void bar();
    }

    void common_foo();
    void a_bar();
    void b_bar();

    struct driver a =
        {
            common_foo,
            a_bar
        };

    struct driver b =
        {
            common_foo,
            b_bar
        };

than the inlined function alternative. Better still, if you need stuff
like that, think C++.

Rup.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]