Why C11 does not support lambda functions

The new C ++ 11 standard supports lambda functions, which I think are a useful feature. I understand that the C and C ++ standards are different from each other, but I do not understand why C11 does not support lambda functions. I think this might come in handy.

Is there a reason why C11 developers do not want to enable this feature?

+6
source share
3 answers

This is really my opinion, because I do not know what the committee is thinking.

On the one hand, Lisp has been supporting lambda expressions since its birth in 1958. The C programming language was born in 1972. So the lambda expression actually has a longer history than C. Therefore, if you ask why C11 does not support the lambda expression, you can ask the same question about C89.

On the other hand, a lambda expression is always a function of functional programming and is gradually absorbed by imperative programming languages. Some of the “higher” languages ​​(like Java, before the planned Java 8) do not yet support it.

Finally, C and C ++ always learn from each other, so maybe it will be in the next C standard. For now, you can take a look at Blocks , a custom extension added by Apple. This is a sample code from Wikipedia:

#include <stdio.h> #include <Block.h> typedef int (^IntBlock)(); IntBlock MakeCounter(int start, int increment) { __block int i = start; return Block_copy( ^ { int ret = i; i += increment; return ret; }); } int main(void) { IntBlock mycounter = MakeCounter(5, 2); printf("First call: %d\n", mycounter()); printf("Second call: %d\n", mycounter()); printf("Third call: %d\n", mycounter()); /* because it was copied, it must also be released */ Block_release(mycounter); return 0; } /* Output: First call: 5 Second call: 7 Third call: 9 */ 
+3
source

C is a small and simple language. It deliberately skips high-level functions when the same things can be done using simpler means. It is intended to provide only the basic functions that are absolutely necessary for portable programming.

C has no links because they are just pointers. C does not have classes, inheritance, or virtual functions, because you can just use structures and create vtables yourself using function pointers. It does not have a garbage collector, because programmers can track memory allocations themselves, it does not have templates, because they are really just macros. If you need exceptions, you can use longjmp, and instead of namespaces, you simply add prefixes to the names.

Adding any of these high-level shortcuts may make programming a little more convenient, but this is because the language is becoming more complex, which should not be underestimated. This is a slippery slope that directly leads to the mess that C ++ has become.

C does not have lambda functions because they are not needed. Instead, you can simply use the static function and put the context in the structure.

+8
source

2016 update: Apple-style apple apples with closure were again presented to the Working Group at a meeting in London in 2016 in a new application document that is trying to address some of the shortcomings of the previous attempt, to legitimize terminology and explanations, and delve into more details about both closures and lambdas can be made "C-like".

Since the reception was cautiously positive (7-0-9 Yes / No / Abstained), it is very likely that something similar to this will soon pass into the language.


The short answer is that C does not include lambda functions because no one has yet made an acceptable proposal for the ISO C working group to include lambda functions.

You can take a look at the list of some of the proposals discussed by the working group here: http://www.open-std.org/jtc1/sc22/wg14/www/documents

The only suggestion for lambdas of any type that I can find on this list is the Apple blocks (as shown in Yu Hao's answer) in document N1451 . This proposal is discussed further in N1483 , which compares it with C ++ lambdas and N1493 and N1542 , which are the minutes of the meetings at which these documents were presented.

There were several reasons why proposal N1451 could not be accepted, given in N1542:

  • initially the committee had difficulty understanding the proposal
  • it uses incorrect quotes and terminology that are contrary to the existing C standard
  • he seems vague and incomplete
  • Apple tried to patent this feature (it is not clear whether this is an obstacle to standardization or not, but I would suggest so)
  • A completely new feature with completely new semantics proposed in 2010 had an absolutely zero chance of being ready for success for 2011 and would delay the release of C11
  • Blocks as presented are not compatible with C ++ 11 lambdas

It seems they are not convinced that it is currently demonstrating sufficient utility. C standardization seems to be trying to be very conservative, and with only one major compiler implementing this function, it is likely that they will want to wait and see how it competes with C ++ lambdas, and someone else picks it up. This is not really a "C" function, not a "Clang" function, until several compilers offer it.

All that was said, the votes of the committee members seemed to have softened slightly in favor of this function (6-5-4 Yes / No / Abstained), but not enough to take the necessary consensus to include it.

As far as I can tell, the other big one, C ++ 11 lambdas, was not suggested for inclusion in C by anyone; and if you do not ask what you will not receive.

Any suggestion for lambdas in C will add a whole host of new rules about lifetimes and locations of variables, as well as copying and distribution, etc. For many people, this potentially starts to look very non-C-like, with values ​​being moved behind the programmer, or sudden unexpected changes in their life expectancy - avoiding these kinds of things, this is half the reason people prefer to write C today. Thus, there should also be a proposal that actually corresponds to the concept of “philosophy” before one can take it seriously. I am sure that this can be done, but both big sentences have so far been developed for languages ​​with a completely different “philosophy”, where such things are less obstructing and do not necessarily reflect the purpose and character of C as they currently stand.

+7
source

Source: https://habr.com/ru/post/951394/


All Articles