Do we have closures in C++?

前端 未结 6 596
慢半拍i
慢半拍i 2020-12-13 02:19

I was reading about closures on the net. I was wondering if C++ has a built-in facility for closures or if there is any way we can implement closures in C++?

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 02:44

    Strictly speaking. 'Closure' is LISP only. Use Let returns lambda as last commands. 'Let Over Lambda'. This is possible only for LISP because of infinite scope with lexical scoping. I don't know any other language support this natively until know.

    (defun my-closure ()
       (let ((cnt 0))
          (lambda () 
             (format t "called : ~A times" (incf cnt)))))
    

提交回复
热议问题