std::function 作为关联容器中的键
std::function as key in associative containers
我的理解是否正确,即不能将 std::function
用作既没有有序的键,即 std::map
、std::set
,也不能使用无序的关联容器,即 std::unordered_map
, std::unordered_set
?
背景是我实现了一个计时器驱动的观察者,即在本例中为主题。我给观察者一个 std::function<void(void)>
对象,以便再次取消订阅。当然,当调用此退订功能时,主题必须保留哪个回调应该unhooked/deleted。
所以我很自然地考虑使用 std::function
的副本作为 std::map
或 std::unordered_map
的键。希望函数指针像语义一样,我希望等于运算符,std::function
实例化的 less 或 hash 实现。我在阅读 reference/the 标准时遇到的令人困惑的事情是:operator==
和 operator!=
没有按预期工作,因为它们只与一个空的即默认构造的函数对象进行比较。
然而,当我阅读 23.14.7 和我的一般理解时,我应该能够在提到的容器中使用开箱即用的 std::function
实例化,因为它们使用 std::equal_to
而不是直接 ==
对吗?
还有标识,即 std::equal_to
/total 顺序会将两个函数对象 "pointing" 匹配到相同的回调,对吧?
Background is that I implement a timer driven observer, i.e. in this case the subject. I give the observers a std::function object in order to unsubscribe again.
不要那样做。
相反,return 指向您存储订阅者可用于取消订阅的函数对象的容器的迭代器。
I should be able to use std::function instantiations out of the box in the mentioned containers, since they using std::equal_to instead of directly == right?
泛型std::equal_to
直接使用运算符==
。 std::function
没有专门化。因此,在 std::function
.
的情况下,使用 ==
和 std::equal_to
没有区别
结论:std::function
不能用作关联容器中的键。而且也没有合理的方法让它与自定义比较函数一起工作。
Damn, why the hell is then std::function implementing an std::hash, a total order guaranteed less operator and even arithmatic operators?
std::function
没有实现其中任何一个。
我的理解是否正确,即不能将 std::function
用作既没有有序的键,即 std::map
、std::set
,也不能使用无序的关联容器,即 std::unordered_map
, std::unordered_set
?
背景是我实现了一个计时器驱动的观察者,即在本例中为主题。我给观察者一个 std::function<void(void)>
对象,以便再次取消订阅。当然,当调用此退订功能时,主题必须保留哪个回调应该unhooked/deleted。
所以我很自然地考虑使用 std::function
的副本作为 std::map
或 std::unordered_map
的键。希望函数指针像语义一样,我希望等于运算符,std::function
实例化的 less 或 hash 实现。我在阅读 reference/the 标准时遇到的令人困惑的事情是:operator==
和 operator!=
没有按预期工作,因为它们只与一个空的即默认构造的函数对象进行比较。
然而,当我阅读 23.14.7 和我的一般理解时,我应该能够在提到的容器中使用开箱即用的 std::function
实例化,因为它们使用 std::equal_to
而不是直接 ==
对吗?
还有标识,即 std::equal_to
/total 顺序会将两个函数对象 "pointing" 匹配到相同的回调,对吧?
Background is that I implement a timer driven observer, i.e. in this case the subject. I give the observers a std::function object in order to unsubscribe again.
不要那样做。
相反,return 指向您存储订阅者可用于取消订阅的函数对象的容器的迭代器。
I should be able to use std::function instantiations out of the box in the mentioned containers, since they using std::equal_to instead of directly == right?
泛型std::equal_to
直接使用运算符==
。 std::function
没有专门化。因此,在 std::function
.
==
和 std::equal_to
没有区别
结论:std::function
不能用作关联容器中的键。而且也没有合理的方法让它与自定义比较函数一起工作。
Damn, why the hell is then std::function implementing an std::hash, a total order guaranteed less operator and even arithmatic operators?
std::function
没有实现其中任何一个。