Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #74775

Re: CAlive to introduce flowofs

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: CAlive to introduce flowofs
Date 2015-11-04 08:57 +1300
Message-ID <d9shsiFs05aU1@mid.individual.net> (permalink)
References (3 earlier) <n18jjk$heh$1@dont-email.me> <d9q30lF7v31U5@mid.individual.net> <n18ljs$qqt$1@dont-email.me> <d9q5tpF7v31U7@mid.individual.net> <n1aa0n$2ud$1@dont-email.me>

Show all headers | View raw


Bartc wrote:
> On 02/11/2015 22:20, Ian Collins wrote:
>>
>> One form would be:
>>
>> struct my_function
>> {
>>     int result;
>>     int count;
>>     int value;
>>
>>     void five_count()
>>     {
>>       result = 5*value;
>>     }
>>
>>     void ten_count()
>>     {
>>       result = 10*value;
>>     }
>>
>>     void greater_than_ten_count()
>>     {
>>       result = 42*value;
>>     }
>>
>>     int operator()( int c, int v )
>>     {
>>       count = c;
>>       value = v;
>>
>>       if (count < 5)         five_count();
>>       else if (count < 10)   ten_count();
>>       else                   greater_than_ten_count();
>>
>>       return result;
>>     }
>> };
>>
>
> That didn't look too bad at first: a function containing local
> functions, not quite what the thread** is about, but it will do the job.
>
> Until I looked closer and realised this wasn't a function, but a struct,
> and what looks like the function body is an operator overload for ().
> Now, I have to think very hard about what it means, how it would be
> used, and why you would do it this way.

It is idiomatic C++, so anyone familiar with the language would know the 
pattern.  As to why, the most common use case is where you want to pass 
something that acts like a function but requires local state.  A simple 
case might be finding all items with a given name in a collection.

-- 
Ian Collins

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

CAlive to introduce flowofs "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-02 11:20 -0800
  Re: CAlive to introduce flowofs "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-02 11:27 -0800
    Re: CAlive to introduce flowofs "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-02 11:34 -0800
  Re: CAlive to introduce flowofs "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-02 11:57 -0800
  Re: CAlive to introduce flowofs supercat@casperkitty.com - 2015-11-02 12:18 -0800
    Re: CAlive to introduce flowofs "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-02 12:24 -0800
      Re: CAlive to introduce flowofs "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-02 12:48 -0800
    Re: CAlive to introduce flowofs "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-02 12:40 -0800
  Re: CAlive to introduce flowofs Bartc <bc@freeuk.com> - 2015-11-02 21:08 +0000
    Re: CAlive to introduce flowofs Ian Collins <ian-news@hotmail.com> - 2015-11-03 10:13 +1300
      Re: CAlive to introduce flowofs Bartc <bc@freeuk.com> - 2015-11-02 21:14 +0000
        Re: CAlive to introduce flowofs Ian Collins <ian-news@hotmail.com> - 2015-11-03 10:31 +1300
          Re: CAlive to introduce flowofs Bartc <bc@freeuk.com> - 2015-11-02 21:48 +0000
            Re: CAlive to introduce flowofs Ian Collins <ian-news@hotmail.com> - 2015-11-03 11:20 +1300
              Re: CAlive to introduce flowofs "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-02 14:33 -0800
                Re: CAlive to introduce flowofs "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-02 15:03 -0800
                Re: CAlive to introduce flowofs Ian Collins <ian-news@hotmail.com> - 2015-11-03 12:05 +1300
                Re: CAlive to introduce flowofs "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-02 15:08 -0800
                Re: CAlive to introduce flowofs supercat@casperkitty.com - 2015-11-02 16:44 -0800
                Re: CAlive to introduce flowofs "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-02 17:20 -0800
                Re: CAlive to introduce flowofs Ian Collins <ian-news@hotmail.com> - 2015-11-03 17:06 +1300
                Re: CAlive to introduce flowofs "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-02 20:13 -0800
              Re: CAlive to introduce flowofs Bartc <bc@freeuk.com> - 2015-11-03 12:43 +0000
                Re: CAlive to introduce flowofs David Brown <david.brown@hesbynett.no> - 2015-11-03 14:20 +0100
                Re: CAlive to introduce flowofs Ian Collins <ian-news@hotmail.com> - 2015-11-04 08:57 +1300
      Re: CAlive to introduce flowofs "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-02 14:31 -0800

csiph-web