News for hackers. Stuff that matters.
Re:C++ Versus Objective-C by blago on Sunday December 11, 2011 @02:14PM (#3340755)
This is like saying that C++ developers are shallow because the language doesn't have closures.
Re:C++ Versus Objective-C by Figs on Sunday December 11, 2011 @02:28PM (#3340799)
C++11 has closures... :)
Re:C++ Versus Objective-C by thurn on Sunday December 11, 2011 @04:19PM (#3341128)
Only for lambdas. There's still no ability to define nested functions as closures, which is arguably more important.
Re:C++ Versus Objective-C by Figs on Sunday December 11, 2011 @09:56PM (#3342040)
What do you mean? You can do this just fine:

    #include <cstdio>
    #include <functional>

    using std::printf;
    using std::function;


    function<int ()> f(int x)
    {
        function<int ()> g = [x]()
        {
            return 2 * x;
        };
        
        return g;
    }


    int main()
    {
        auto f1 = f(5);
        auto f2 = f(6);
        
        printf("f1() = %d, f2() = %d\n", f1(), f2());
        
        return 0;
    }
(Which prints `f1() = 10, f2() = 12`)

Or did you mean nested functions in the sense of being able to form named closures so that they can call themselves with a headache? It is admittedly more or a pain to do that (if you want to return a closure), but it is still doable, using new and delete:

    #include <cstdio>
    #include <functional>

    using std::printf;
    using std::function;


    function<int (int)>* f(int x)
    {
        function<int (int)>* g = new function<int (int)>();
        
        *g = [g, x](int n)
        {
            if(n <= 0)
                return 1;
            else
                return x + n * (*g)(n-1);
        };
        
        return g;
    }


    int main()
    {
        auto f1 = f(0);
        auto f2 = f(1);
        
        printf("f1() = %d, f2() = %d\n", (*f1)(5), (*f2)(5));
        
        delete f1;
        delete f2;
        
        return 0;
    }
Or did you mean something else?

Edit: Or, you could do something Y-combinator-ish like this: http://rosettacode.org/wiki/Y_combinator#C.2B.2B but yeah, it is a bit of a hassle compared to named nested functions.

Re:C++ Versus Objective-C by blago on Sunday December 11, 2011 @03:01PM (#3340893)
Welcome to the future. We'll through you a party :-)