template <typename a, typename ... b>
class events
{
public:
    typedef std::function<void( a arg, b ... argx)> OnSomethingHandler;

    events(OnSomethingHandler Handler)
    {
        handlers_.push_back(Handler);
    }


    void operator ()()
    {
        for(auto i = handlers_.begin(); i != handlers_.end(); ++i)
            (*i)();
    }

private:
    std::vector<OnSomethingHandler> handlers_;

    void AddHandler(OnSomethingHandler Handler)
    {
        handlers_.push_back(Handler);
    }

    void TriggerEvents()
    {
        for(auto i = handlers_.begin(); i != handlers_.end(); ++i)
            (*i)();
    }

};

how can i had paramaters to lambda?

i'm trying, but i get errors:

int main()

{

    events my_foo([](int a, int b){
        std::cout << (a+b) << std::endl;
    });


    my_foo(4,5);

    return 0;
}

errors messages:

"C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp||In function 'int main()':|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|46|error: missing template arguments before 'my_foo'|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|46|error: expected ';' before 'my_foo'|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|48|error: expected primary-expression before ')' token|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|48|error: expected ';' before ')' token|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|51|error: 'my_foo' was not declared in this scope|
||=== Build finished: 5 errors, 0 warnings (0 minutes, 1 seconds) ===|"

Your operator () is wrong and you also need to tell it what parameters the class takes..

#include <functional>
#include <vector>
#include <iostream>

template<typename... Args>
class events
{
public:
    typedef std::function<void(Args... args)> OnSomethingHandler;

    events(OnSomethingHandler Handler)
    {
        handlers_.push_back(Handler);
    }


    void operator ()(Args... args)
    {
        for(auto i = handlers_.begin(); i != handlers_.end(); ++i)
            (*i)(args...);
    }

private:
    std::vector<OnSomethingHandler> handlers_;

    void AddHandler(OnSomethingHandler Handler)
    {
        handlers_.push_back(Handler);
    }

    void TriggerEvents()
    {
        for(auto i = handlers_.begin(); i != handlers_.end(); ++i)
            (*i)();
    }

};

int main()
{
    events<int, int> foo([](int a, int b){
        std::cout << (a + b) << "\n";
    });

    foo(5, 5);
}

Edited 3 Years Ago by triumphost

i'm testing that code in a class:

#include <iostream>
#include <functional>
#include <vector>

using namespace std;


template<typename... Args>
class events
{
public:
    typedef std::function<void(Args... args)> OnSomethingHandler;
    events(OnSomethingHandler Handler)
    {
        handlers_.push_back(Handler);
    }
    void operator ()(Args... args)
    {
        for(auto i = handlers_.begin(); i != handlers_.end(); ++i)
            (*i)(args...);
    }
private:
    std::vector<OnSomethingHandler> handlers_;
    void AddHandler(OnSomethingHandler Handler)
    {
        handlers_.push_back(Handler);
    }
    void TriggerEvents()
    {
        for(auto i = handlers_.begin(); i != handlers_.end(); ++i)
            (*i)();
    }
};

class test
{
public:
    events<> d;
    d.AddOnSomethingHandler([](){
        std::cout << "Hello, World!" << std::endl;
    });
    test()
    {
        d.TriggerOnSomethingEvents();
    }
};


int main()
{

   cin.get();
}

error messages:

"C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|39|error: 'd' does not name a type|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|41|error: expected unqualified-id before ')' token|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp||In constructor 'test::test()':|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|43|error: no matching function for call to 'events<>::events()'|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|43|note: candidates are:|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|13|note: events<Args>::events(events<Args>::OnSomethingHandler) [with Args = {}; events<Args>::OnSomethingHandler = std::function<void()>]|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|13|note: candidate expects 1 argument, 0 provided|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|9|note: events<>::events(const events<>&)|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|9|note: candidate expects 1 argument, 0 provided|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|9|note: events<>::events(events<>&&)|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|9|note: candidate expects 1 argument, 0 provided|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|44|error: 'class events<>' has no member named 'TriggerOnSomethingEvents'|
||=== Build finished: 4 errors, 0 warnings (0 minutes, 1 seconds) ===|
"
why these errors if works fine in function main()?

Edited 3 Years Ago by cambalinho

finnaly the code is done... now the C++ programmers can do nice events:

//events.h
#ifndef events_H_INCLUDED
#define events_H_INCLUDED

#include <functional>
#include <vector>

template <typename ... b>
class events
{
public:
    typedef std::function<void(b...argx )> OnSomethingHandler;

    events(OnSomethingHandler Handler)
    {
        handlers_=Handler;
    }

    void operator ()(b... args)
    {
        handlers_(args...);
    }
    events& operator = (OnSomethingHandler Handler)
    {
        handlers_ = Handler;
        return *this;
    }

private:
    OnSomethingHandler handlers_;

};

#endif // events_H_INCLUDED

and the class test:

#include <iostream>
#include "events.h"

using namespace std;

class test
{
    int x;
    int y;
public:

    events<int, int> moved{[](int a, int b) { ; }}; //the move event
    test(std::function<void(void)> Created): x(20), y(30)//for the create event i use these parameter
    {
        Created();
    }

    void setx(int s)
    {
        x=s;

        moved(x,y); //like you see, i send argurments to the class and they can be diferent from diferent object
    }
};

int main()
{
    //create the object from test class and initializate the create event
    test a([]()
    {
        cout << "hello world" << endl;
    });

    //lets change, for these object, the moved event
    a.moved=[](int a, int b)
    {
        cout << "moved to: x = " << a << " y = " << b;

    };

    //lest change the x position
    a.setx(100);

   cin.get();
}

i accept sugestions and tips ;)
thanks to all

please someone see these code(again):

#ifndef events_H_INCLUDED
#define events_H_INCLUDED

#include <functional>
#include <vector>


template <class ... b>
class events
{
public:
    typedef std::function<void(b...argx )> OnSomethingHandler;

    events(OnSomethingHandler Handler)
    {
        handlers_=Handler;
    }

    void operator ()(b... args)
    {
        handlers_(args...);
    }
    events& operator = (OnSomethingHandler Handler)
    {
        handlers_ = Handler;
        return *this;
    }

private:
    OnSomethingHandler handlers_;

};

#endif // events_H_INCLUDED

can i overload the '()' operator for use it ouside of main()?

This question has already been answered. Start a new discussion instead.