Functor with binded params is wrong

2008-10-22
2013-04-08
  • Sergiy Korobov
    Sergiy Korobov
    2008-10-22

    I have some issues when I'm invoking a generic functor with binded string params. This functor is wrong only when it has been returned from some function. I use VC++ 2005.

    #include <iostream>
    #include <stdio.h>
    #include <string>

    #include <loki/Functor.h>
    #include <loki/HierarchyGenerators.h>

    using namespace Loki;

    typedef std::string String;

    struct SomeClass
    {
        void out(const String &a, const String &b)
        {
            std::cout << a << " " << b << std::endl;
        }
    };

    SomeClass sc;

    Functor<> getFunctor()
    {
        String hello("Hello"), world("world!");
        Functor<void, LOKI_TYPELIST_2(String, String)> cmd1(&sc, &SomeClass::out);
        Functor<void, LOKI_TYPELIST_1(String)> cmd2(BindFirst(cmd1, hello));
        Functor<> cmd3(BindFirst(cmd2, world));
        return cmd3;
    }

    int main(int, char **)
    {
        String hello("Hello"), world("world!");
        Functor<void, LOKI_TYPELIST_2(String, String)> cmd1(&sc, &SomeClass::out);
        Functor<void, LOKI_TYPELIST_1(String)> cmd2(BindFirst(cmd1, hello));
        Functor<> cmd3(BindFirst(cmd2, world));
        cmd3(); // good
        std::cin.get();

        cmd3 = getFunctor();
        cmd3(); // bad
        std::cin.get();
        return 0;
    }