Do we have closures in C++?

C++ClosuresLexical Closures

C++ Problem Overview


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++?

C++ Solutions


Solution 1 - C++

Solution 2 - C++

If you understand closure as a reference to a function that has an embedded, persistent, hidden and unseparable context (memory, state), then yes:

class add_offset {
private:
    int offset;
public:
    add_offset(int _offset) : offset(_offset) {}
    int operator () (int x) { return x + offset; }
}

// make a closure
add_offset my_add_3_closure(3);

// use closure
int x = 4;
int y = my_add_3_closure(x);
std::cout << y << std::endl;

The next one modifies its state:

class summer
{
private:
    int sum;
public:
    summer() : sum(0) {}
    int operator () (int x) { return sum += x; }
}

// make a closure
summer adder;
// use closure
adder(3);
adder(4);
std::cout << adder(0) << std::endl;

The inner state can not be referenced (accessed) from outside.

Depending on how you define it, a closure can contain a reference to more than one function or, two closures can share the same context, i.e. two functions can share the same persistent state.

Closure means not containing free variables - it is comparable to a class with only private attributes and only public method(s).

Solution 3 - C++

Yes, This shows how you could implement a function with a state without using a functor.

#include <iostream>
#include <functional>
 

std::function<int()> make_my_closure(int x){
    return [x]() mutable {   
        ++x;
        return x;   
    };
}

int main()
{
    auto my_f = make_my_closure(10);
    
    std::cout << my_f() << std::endl; // 11
    std::cout << my_f() << std::endl; // 12
    std::cout << my_f() << std::endl; // 13
    
     auto my_f1 = make_my_closure(1);

    std::cout << my_f1() << std::endl; // 2
    std::cout << my_f1() << std::endl; // 3
    std::cout << my_f1() << std::endl; // 4

    std::cout << my_f() << std::endl; // 14
}

I forgot the mutable keyword which introduced an undefined behaviour (clang version was returning a garbage value). As implemented, the closure works fine (on GCC and clang)

Solution 4 - C++

I suspect that it depends on what you mean by closure. The meaning I've always used implies garbage collection of some sort (although I think it could be implemented using reference counting); unlike lambdas in other languages, which capture references and keep the referenced object alive, C++ lambdas either capture a value, or the object refered to is not kept alive (and the reference can easily dangle).

Solution 5 - C++

Yes, C++11 has closures named lambdas.

In C++03 there is no built-in support for lambdas, but there is Boost.Lambda implementation.

Solution 6 - C++

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)))))

Solution 7 - C++

You can achive similar functionality using static variables and lambdas.

#include <iostream>
#include<functional>

int main()
{
    std::function<std::function<int()>()> generator_function=[]()->std::function<int()>{
        static int i=0;
        return [&]()->int{
            return i++;
        };
    };
    
    std::function<int()> iterator_function=generator_function();
    
    std::cout<<iterator_function()<<std::endl;  //1
    std::cout<<iterator_function()<<std::endl;  //2
    std::cout<<iterator_function()<<std::endl;  //3 
    std::cout<<iterator_function()<<std::endl;  //4
    return 0;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionParvinder SinghView Question on Stackoverflow
Solution 1 - C++Apeirogon PrimeView Answer on Stackoverflow
Solution 2 - C++ZrinView Answer on Stackoverflow
Solution 3 - C++SetepenreView Answer on Stackoverflow
Solution 4 - C++James KanzeView Answer on Stackoverflow
Solution 5 - C++RostView Answer on Stackoverflow
Solution 6 - C++Mark YangView Answer on Stackoverflow
Solution 7 - C++Amresh KumarView Answer on Stackoverflow