6

I was reading an article on C++11 and the author mentioned that one of the advantages of using lambdas is to avoid the tedium and security risks of function objects.

What are some security risks of FOs?

Source:The Biggest Changes in C++11 and Why You should Care

Karthik T
  • 31,456
  • 5
  • 68
  • 87
Justin
  • 447
  • 4
  • 10
  • 33
  • 4
    You mention function pointers in the title but you're asking about function objects (functors) in the text. Which is it? – Anthill Nov 27 '12 at 06:57
  • 6
    @ValekHalfHeart : Parameters for `operator()` work no differently than for normal functions -- how could type safety possibly be affected? – ildjarn Nov 27 '12 at 06:58
  • @idljarn Perhaps I should clarify: Function pointers themselves are type safe, but many situations arise where they indirectly can lead to type-unsafe behavior. [This post](http://stackoverflow.com/questions/10237847/how-are-function-pointers-type-unsafe) provides some useful insight into what I'm saying. – ApproachingDarknessFish Nov 27 '12 at 07:43
  • 1
    @ValekHalfHeart I think it's `void*` and casting that can lead to type-unsafe behaviour, not function pointers themselves. (And that's what your linked post says, aparrently) – Kos Nov 27 '12 at 08:12
  • Let's put the question this way: Are lambdas and `std::function` objects more type safe than functors and function pointers? – Kos Nov 27 '12 at 08:16
  • I suppose lambdas aren't more safe than function pointers (as operator () is indeed a member function) but lambdas may be more safe than functors. lambdas provides automatic capture of all variables participating in lambda expression; in case of functors the corresponding task should be performed by hand. It's a tedious work and may produce bugs, such as uninitialized or improperly initialized data members of the functor. – user396672 Nov 27 '12 at 08:47

0 Answers0