set of functions

Consider this code:

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
int main()
{
	typedef std::function<void()> tvFun;
	std::set<tvFun> sFun;

	foo1 f1(3, 8.5);
	foo2 f2(9, 1.1);
	foo3 f3(21, 7.8);
	foo4 f4(44, 3.2);

	sFun.insert(f1);
	sFun.insert(f3);
	sFun.insert(f2);
	sFun.insert(f4);

	auto it         = sFun.begin();
	const auto& end = sFun.end();

	for(; it != end; ++it)
	{
		(*it)();
	}

	return 0;
}


Error output:

error C2784: 'bool std::operator <(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'const tvFun'


There is a problem with Compare function. How do i make set containing functions?
Last edited on
It seems that you are typedef'fing a template that takes a function with no arguments, but are later pushing functions onto the stack that take ints and floats:

1
2
3
4
5
6
7
8
9
10
typedef std::function<void()> tvFun; //takes a a template argument a void(), 
//with no function arguments

//then you try to add members to the list that take an int and a float as a signature

        foo1 f1(3, 8.5);
	foo2 f2(9, 1.1);
	foo3 f3(21, 7.8);
	foo4 f4(44, 3.2);


Maybe this?
 
typedef std::function<void(int,float)> tvFun;
Last edited on
No, no, foo's are functor classes:

1
2
3
4
5
6
7
8
9
10
11
12
class foo1
{
	int data1;
	double data2;
public:
	foo1(int d1, double d2) : data1(d1), data2(d2) {}
	void operator () ()
	{
		// do something with data1 and data2
   
	}
};


This was bad example to explain my problem.

They could have different constructor and data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
foo1 f1(5, "hello", 3.2f);
foo2 f2();
foo3 f3(4.5);
foo4 f4("doo", "daa");

sFun.insert(f1);
	sFun.insert(f3);
	sFun.insert(f2);
	sFun.insert(f4);

...
for(; it != end; ++it)
	{
		(*it)(); // call their operator ()
	}


I need set of unique foo's.
What should i do for sets Compare for this to work?
Last edited on
Whatever you put in std::set, you must provide a comparison function, or more naturally add an operator<.

Example (not tested):
1
2
3
4
5
6
7
8
class foo1 {
public:

    bool operator < (const foo1 &f) const
    {
        return this < &foo1;
    }
};

That's not it.

Whole code to test:
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <string>
#include <functional>
#include <set>
#include <vector>
#include <iterator>

class foo1
{
	int    data1;
	double data2;

public:
	foo1(int d1, double d2) : data1(d1), data2(d2) {}

	void operator () ()
	{
		std::cout << "foo1 data1 = " << data1 << ", data2 = " << data2 << std::endl;
	}
};

class foo2
{
	std::string data;
public:
	foo2(std::string d) : data(d) {}

	void operator () ()
	{
		std::cout << "foo2 data1 = " << data << std::endl;
	}
};

class foo3
{
	float       data1;
	std::string data2;
	int         data3;
public:
	foo3(float d1, std::string d2, int d3) : data1(d1), data2(d2), data3(d3) {}
	void operator () ()
	{
		std::cout << "foo3 data1 = " << data1 << ", data2 = " << data2 << ", data3 = " << data3 << std::endl;
	}
};

class foo4
{
public:
	foo4() {}

	void operator () ()
	{
		std::cout << "foo4" << std::endl;
	}
};

int main()
{
	foo1 f1(3, 8.5);
	foo2 f2("hello");
	foo3 f3(2.8f, "doo", 4);
	foo4 f4;

	typedef std::function<void()> tFun;
	
#if 0 // use set
	std::set<tFun> sFun;
	sFun.insert(f1);
	sFun.insert(f3);
	sFun.insert(f2);
	sFun.insert(f4);
#else // use vector
	std::vector<tFun> sFun;
	sFun.push_back(f1);
	sFun.push_back(f2);
	sFun.push_back(f3);
	sFun.push_back(f4);
#endif

	auto it         = sFun.begin();
	const auto& end = sFun.end();

	for(; it != end; ++it)
	{
		(*it)();
	}

	return 0;
}
Last edited on
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <string>
#include <functional>
#include <set>
#include <vector>
#include <iterator>

class foo1
{
	int    data1;
	double data2;

public:
	foo1(int d1, double d2) : data1(d1), data2(d2) {}

	void operator () ()
	{
		std::cout << "foo1 data1 = " << data1 << ", data2 = " << data2 << std::endl;
	}
};

class foo2
{
	std::string data;
public:
	foo2(std::string d) : data(d) {}

	void operator () ()
	{
		std::cout << "foo2 data1 = " << data << std::endl;
	}
};

class foo3
{
	float       data1;
	std::string data2;
	int         data3;
public:
	foo3(float d1, std::string d2, int d3) : data1(d1), data2(d2), data3(d3) {}
	void operator () ()
	{
		std::cout << "foo3 data1 = " << data1 << ", data2 = " << data2 << ", data3 = " << data3 << std::endl;
	}
};

class foo4
{
public:
	foo4() {}

	void operator () ()
	{
		std::cout << "foo4" << std::endl;
	}
};

typedef std::function<void()> tFun;

bool compareFunction(const tFun &tf1, const tFun &tf2)
{
	return &tf1 < &tf2;
}

int main()
{
	foo1 f1(3, 8.5);
	foo2 f2("hello");
	foo3 f3(2.8f, "doo", 4);
	foo4 f4;

#if true // use set
	std::clog << "USING SET" << std::endl;

	// C++98 style
	//std::set<tFun, bool (*)(const tFun &tf1, const tFun &tf2)> sFun(compareFunction);

	// C++11 style
	std::set<tFun, bool (*)(const tFun &tf1, const tFun &tf2)> sFun([](const tFun &tf1, const tFun &tf2) -> bool {
		return &tf1 < &tf2;
	});

	sFun.insert(f1);
	sFun.insert(f3);
	sFun.insert(f2);
	sFun.insert(f4);

#else // use vector
	std::clog << "USING VECTOR" << std::endl;

	std::vector<tFun> sFun;
	sFun.push_back(f1);
	sFun.push_back(f2);
	sFun.push_back(f3);
	sFun.push_back(f4);
#endif

	auto it         = sFun.begin();
	const auto& end = sFun.end();

	for(; it != end; ++it)
	{
		(*it)();
	}

	return 0;
}

Still adds duplicates:

1
2
3
4
5
sFun.insert(f1);
	sFun.insert(f3);
	sFun.insert(f2);
	sFun.insert(f4);
	sFun.insert(f3);// duplicate but added 


Solved it with vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
typedef std::function<void()> tFun;
std::vector<tFun> sFun;
...
void addFun(std::function<void()> f)
{
auto it = std::find_if(std::begin(sFun), std::end(sFun), [&f](const std::function<void()>& o){
  return o.target_type() == f.target_type() ;
});
if(it == sFun.end())
  sFun.push_back(f);
}
...
addFun(f1);
addFun(f2);
addFun(f3);
addFun(f1); // duplicate not added
addFun(f4);
addFun(f2);// duplicate not added 
Last edited on
Interesting. I wonder why it adds duplicates.

Wait. There's a cast going on.
Last edited on
morando wrote:
return o.target_type() == f.target_type()

If you're looking for unique target_types, you could use before() in the set's comparison functor
return o.target_type().before(f.target_type()); or even go through type_index if you have it.
Last edited on
Topic archived. No new replies allowed.