爲什麼std :: function不相等?
我認爲主要的原因是,如果它是,那麼它不能用於非平等可比類型,即使從不執行平等比較。
I.e.執行比較的代碼應該提前實例化 - 當可調用對象存儲到std :: function中時,例如在構造函數或賦值運算符中。
這樣的限制將大大縮小應用範圍,並且顯然不被「通用多態函數包裝器」所接受。
這是improtant要注意,有可能compare boost::function與調用對象(但不與其它的boost ::功能)
函數對象包裝可以通過==進行比較或!=針對任何可以存儲在包裝器中的函數對象。
這是可能的,因爲執行這種比較的函數是基於已知操作數類型在比較點處即時的。
此外,std :: function有target template member function,它可以用來執行類似的比較。實際上boost :: function的比較運算符是implemented in terms of target member function。
因此,沒有技術障礙阻止實施function_comparable。
其中的答案有共同的模式「一般是不可能的」:
-
即使到那時,你會得到平等的狹義的概念,等效功能會比較,如果不相等(爲例如)它們是通過以不同順序綁定參數來構造的。我認爲在一般情況下測試等同性是不可能的。
-
我可能是錯的,但我認爲,平等是性病::函數對象是可惜不是一般意義上的解。
-
因爲圖靈機的等價性是不可判定的。給定兩個不同的函數對象,你不可能確定它們是否計算相同的函數。 [這個答案被刪除]
我完全不同意這種觀點:它不是的std ::功能的工作進行比較本身,它的工作就是重定向要求比較底層的對象 - 就這樣。
如果底層對象類型沒有定義比較 - 在任何情況下都是編譯錯誤,不需要使用std :: function來推導比較算法。
如果底層對象類型定義了比較,但是其中的工作不正確,或者有一些不尋常的語義 - 它不是std :: function本身的問題,但它是底層類型的問題。
有可能基於的std ::函數來實現function_comparable。
這裏是證明了概念:
template<typename Callback,typename Function> inline
bool func_compare(const Function &lhs,const Function &rhs)
{
typedef typename conditional
<
is_function<Callback>::value,
typename add_pointer<Callback>::type,
Callback
>::type request_type;
if (const request_type *lhs_internal = lhs.template target<request_type>())
if (const request_type *rhs_internal = rhs.template target<request_type>())
return *rhs_internal == *lhs_internal;
return false;
}
#if USE_VARIADIC_TEMPLATES
#define FUNC_SIG_TYPES typename ...Args
#define FUNC_SIG_TYPES_PASS Args...
#else
#define FUNC_SIG_TYPES typename function_signature
#define FUNC_SIG_TYPES_PASS function_signature
#endif
template<FUNC_SIG_TYPES>
struct function_comparable: function<FUNC_SIG_TYPES_PASS>
{
typedef function<FUNC_SIG_TYPES_PASS> Function;
bool (*type_holder)(const Function &,const Function &);
public:
function_comparable() {}
template<typename Func> function_comparable(Func f)
: Function(f), type_holder(func_compare<Func,Function>)
{
}
template<typename Func> function_comparable &operator=(Func f)
{
Function::operator=(f);
type_holder=func_compare<Func,Function>;
return *this;
}
friend bool operator==(const Function &lhs,const function_comparable &rhs)
{
return rhs.type_holder(lhs,rhs);
}
friend bool operator==(const function_comparable &lhs,const Function &rhs)
{
return rhs==lhs;
}
friend void swap(function_comparable &lhs,function_comparable &rhs)// noexcept
{
lhs.swap(rhs);
lhs.type_holder.swap(rhs.type_holder);
}
};
有一些漂亮的酒店 - function_comparable可以針對的std ::功能也進行比較。
舉例來說,假設我們有vector of std::function's,我們希望給用戶register_callback和unregister_callback功能。的function_comparable使用只爲unregister_callback參數要求:
void register_callback(std::function<function_signature> callback);
void unregister_callback(function_comparable<function_signature> callback);
Live demo at Ideone
演示的源代碼:
// Copyright Evgeny Panasyuk 2012.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <type_traits>
#include <functional>
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <typeinfo>
#include <utility>
#include <ostream>
#include <vector>
#include <string>
using namespace std;
// _____________________________Implementation__________________________________________
#define USE_VARIADIC_TEMPLATES 0
template<typename Callback,typename Function> inline
bool func_compare(const Function &lhs,const Function &rhs)
{
typedef typename conditional
<
is_function<Callback>::value,
typename add_pointer<Callback>::type,
Callback
>::type request_type;
if (const request_type *lhs_internal = lhs.template target<request_type>())
if (const request_type *rhs_internal = rhs.template target<request_type>())
return *rhs_internal == *lhs_internal;
return false;
}
#if USE_VARIADIC_TEMPLATES
#define FUNC_SIG_TYPES typename ...Args
#define FUNC_SIG_TYPES_PASS Args...
#else
#define FUNC_SIG_TYPES typename function_signature
#define FUNC_SIG_TYPES_PASS function_signature
#endif
template<FUNC_SIG_TYPES>
struct function_comparable: function<FUNC_SIG_TYPES_PASS>
{
typedef function<FUNC_SIG_TYPES_PASS> Function;
bool (*type_holder)(const Function &,const Function &);
public:
function_comparable() {}
template<typename Func> function_comparable(Func f)
: Function(f), type_holder(func_compare<Func,Function>)
{
}
template<typename Func> function_comparable &operator=(Func f)
{
Function::operator=(f);
type_holder=func_compare<Func,Function>;
return *this;
}
friend bool operator==(const Function &lhs,const function_comparable &rhs)
{
return rhs.type_holder(lhs,rhs);
}
friend bool operator==(const function_comparable &lhs,const Function &rhs)
{
return rhs==lhs;
}
// ...
friend void swap(function_comparable &lhs,function_comparable &rhs)// noexcept
{
lhs.swap(rhs);
lhs.type_holder.swap(rhs.type_holder);
}
};
// ________________________________Example______________________________________________
typedef void (function_signature)();
void func1()
{
cout << "func1" << endl;
}
void func3()
{
cout << "func3" << endl;
}
class func2
{
int data;
public:
explicit func2(int n) : data(n) {}
friend bool operator==(const func2 &lhs,const func2 &rhs)
{
return lhs.data==rhs.data;
}
void operator()()
{
cout << "func2, data=" << data << endl;
}
};
struct Caller
{
template<typename Func>
void operator()(Func f)
{
f();
}
};
class Callbacks
{
vector<function<function_signature>> v;
public:
void register_callback_comparator(function_comparable<function_signature> callback)
{
v.push_back(callback);
}
void register_callback(function<function_signature> callback)
{
v.push_back(callback);
}
void unregister_callback(function_comparable<function_signature> callback)
{
auto it=find(v.begin(),v.end(),callback);
if(it!=v.end())
v.erase(it);
else
throw runtime_error("not found");
}
void call_all()
{
for_each(v.begin(),v.end(),Caller());
cout << string(16,'_') << endl;
}
};
int main()
{
Callbacks cb;
function_comparable<function_signature> f;
f=func1;
cb.register_callback_comparator(f);
cb.register_callback(func2(1));
cb.register_callback(func2(2));
cb.register_callback(func3);
cb.call_all();
cb.unregister_callback(func2(2));
cb.call_all();
cb.unregister_callback(func1);
cb.call_all();
}
輸出是:
func1
func2, data=1
func2, data=2
func3
________________
func1
func2, data=1
func3
________________
func2, data=1
func3
________________
P.S.看起來在std::type_index的幫助下,可以實現類似於類別function_comparable類別,其也支持排序(即較少)或甚至散列。但不僅是在不同類型之間進行排序,還要在同一類型中進行排序(這需要類型支持,如LessThanComparable)。
請注意,您可以要求'* a.target < ftor_type >()== * b.target < ftor_type >()'是否指向平等可比函子。雖然這有點挑剔(底層對象不會被隱式轉換爲所請求的類型),但它確實指定了正在使用的比較語義。 – Potatoswatter 2012-02-05 09:14:23