my new Event class: template variadics and how test a valid function\lambda

Pages: 12
see my class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
template<typename...a>
class Event
{
public:
    std::function<void()> EventName{[](){;}};
    std::function<void(a...parameters)> EventNameWithParameters{[](a...parameters){;}};

    Event(std::function<void()> Event=[](){;})
    {
        EventName=Event;
    }

    Event&operator ()()
    {
        EventName();
        return *this;
    }

    Event(std::function<void(a...parameters)> Event=[](a...parameters){;})
    {
        EventNameWithParameters=Event;
    }

    Event &operator ()(a...parameters)
    {
        EventNameWithParameters(parameters...);
        return *this;
    }
};


how i can use it(with and without parameters):

Event test{[](){MessageBox("hello world");}}
i get these error message: "invalid use of template-name 'Event' without an argument list"
i thot that i was doing rigth with:
template<typename...a>
what i'm doing wrong?
my objective is accept any or more parameters
Last edited on
template<typename...a> allows no paramter. So you do not need to do any extra work for this.

Change

Event test{[](){MessageBox("hello world");}}

to:

Event<> test{[](){MessageBox("hello world");}} // Note: <>


What is the use of the empty lambdas?
"What is the use of the empty lambdas?"
just an inicializate value
i have another error here:
Event(std::function<void(a...parameters)> Event=[](a...parameters){;})
what i'm doing wrong?
Remove the extra code for the function without parameter.
yes. now works fine. thanks for all
how can i test if the function is empty or something?
(someone said me before, but i forget it)
1
2
3
    std::function<void()> func;
    if(func) // Note: the operator bool tells whether the function is valid or not
      func();
doing these:
test=[](int, int){;};
means that have a function.
but doing these:
test=NULL
i will get empty.
heres how i test it:
1
2
3
4
5
6
7
bool IsEmpty()
    {
        if (EventNameWithParameters)
            return false;
        else
            return true;
    }

with that 'if', i fixed another confused error. thanks for all
Last edited on
what you can tell me?
what you can tell me?
About what? I don't get that question.
maybe was you that said that i must do these(for be empty or something):
test=[](int, int){;};
but the 'if' calls it anyway:
1
2
3
std::function<void()> test=[](int, int){;};;
    if(test) // Note: the operator bool tells whether the function is valid or not
      test();

that's true don't do anything, but calls it.
so the 'if' only test if the 'test' is good for call nothing more, right?
seen again.. sorry. i read, again the comment.
thaks for all
Last edited on
see the class updated:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
template<typename...a>
class Event
{
public:
    std::function<void(a...parameters)> EventNameWithParameters{[](a...parameters){;}};


    Event(std::function<void(a...parameters)> Event=[](a...parameters){;})
    {
        EventNameWithParameters=Event;
    }

    Event &operator ()(a...parameters)
    {
        if(EventNameWithParameters)
            EventNameWithParameters(parameters...);
        return *this;
    }

    Event &operator =(std::function<void(a...parameters)> Event)
    {
        EventNameWithParameters=Event;
    }

    bool IsValid()
    {
        if (EventNameWithParameters)
            return true;
        else
            return false;
    }
};

//with parameters:
Event<int, int> test{[](int a, int b)
{
    MessageBox("hello world 1: " + to_string(a+b));
}};

//Without parameters
Event<> test2{[]()
{
    MessageBox("hello world!!!!");
}};

Readers: see the diference betwee with parameters and without.
like you see now the function is tested before been called.
maybe was you that said that i must do these
No. It is your program and you must check whether something is usefull within that context or not.

So what do you think? Is it usefull or not?
Consider the effects within your program. Where I don't have the overview.
yes it is, by several reasons:
1 - it's more easy create the lambda;
2 - always test before call it...

well the only problem is that i can't change it on General Area, only create it and inicializate it.
"Consider the effects within your program. Where I don't have the overview."
maybe you are warning me something, but i don't see the bad thing.
why i can't use it on parameter function?
1
2
3
4
Event<> timerprocedure;
    //std::function<void()> timerprocedure=nullptr;
    Timer(Event<> tmrprocedure=[](){;})
    {


error message: "could not convert '<lambda closure object>Timer::__lambda4{}' from 'Timer::__lambda4' to 'Event<>'"
i don't know what means these error... can you explain better?
I would think that the implicit conversion is not possible since the lambda function is a pointer. Try:

Timer(Event<> tmrprocedure([](){;})) // Note the extra ()
Timer(Event<> tmrprocedure([](){;}))
error message:
"expected ',' or '...' before '(' token"
why if i don't use arguments?
What are you trying to do? Defining a parameter with the same name or passing a paramter?

If you want to define a [default] paramer, it would look like this:

Timer(Event<> tmrprocedure = Event<>()) // You have already the empty lamda a default in Event
yes it a default constructor.

i did. so see these Timer sample\object:
Timer tmrCreate{[&](){Create();}};
error:
could not convert '{<lambda closure object>consolewindow::__lambda144{((consolewindow*)this)}}' from '<brace-enclosed initializer list>' to 'Timer'
why these error?
Since it is not possible to cast a lamda implicitly you nee to do it explicitly:

Timer tmrCreate{Event<>([&](){Create();})};
Pages: 12