Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么std::function不具有可比性?_C++_Function_Boost_C++11_Tr1 - Fatal编程技术网

C++ 为什么std::function不具有可比性?

C++ 为什么std::function不具有可比性?,c++,function,boost,c++11,tr1,C++,Function,Boost,C++11,Tr1,这个问题也适用于boost::function和std::tr1::function std::function不具有可比性: #include <functional> void foo() { } int main() { std::function<void()> f(foo), g(foo); bool are_equal(f == g); // Error: f and g are not equality comparable } 它没有

这个问题也适用于
boost::function
std::tr1::function

std::function
不具有可比性:

#include <functional>
void foo() { }

int main() {
    std::function<void()> f(foo), g(foo);
    bool are_equal(f == g); // Error:  f and g are not equality comparable
}
它没有说明“类型系统中可能存在的漏洞”是什么。在TR1和Boost中,重载是声明的,但没有定义。TR1规范注释(N1836§3.7.2.6):

这些成员函数应保持未定义状态

[注:类似布尔值的转换打开了一个漏洞,通过
=
!=
可以比较两个函数实例。这些未定义的
void
运算符关闭了漏洞,并确保编译时出错。-结束注]

我对“漏洞”的理解是,如果我们有一个
bool
转换函数,该转换可用于等式比较(以及在其他情况下):

我的印象是C++03中的safe bool习惯用法和C++11中显式转换函数的使用都是为了避免这个“漏洞”。Boost和TR1都在
函数中使用safe bool习惯用法,C++11使
bool
转换函数显式化

作为同时具有这两种功能的类的一个示例,
std::shared_ptr
都具有显式的
bool
转换函数,并且具有相等可比性

为什么
std::function
不具有可比性?“类型系统中可能存在的漏洞”是什么?它与
std::shared\u ptr

有何不同

这里的主要评论是本文的一部分
std::function
的历史记录 是用N1402引入的。其间 时间无显式转换函数 存在,以及“安全布尔”成语 (基于指向成员的指针)是 流行技术。唯一的 这个成语的缺点是 给定类型为的两个对象
f1
f2
std::function
,表达式

f1 == f2;
只是因为 内置的
运算符==
用于指向 经过一个月的审议后,该成员得到了考虑 用户定义的转换。为了解决这个问题, 未定义的重载集 增加了比较函数,如 这是我所希望的 最终导致链接错误。 已删除的新语言工具 功能提供了更好的解决方案 修复此问题的诊断机制 问题

在C++11中,由于引入了显式转换运算符,被删除的函数被认为是多余的,因此C++11可能会删除这些函数

这个问题的中心是,, 那是因为更换了 通过显式转换实现安全布尔习语 对于
bool
,类型中的原始“孔” “系统”不再存在,并且 因此,该评论是错误的,并且 多余函数定义 也应该被移除


至于为什么不能比较
std::function
对象,可能是因为它们可能包含全局/静态函数、成员函数、函子等,而要做到这一点
std::function
会“擦除”有关底层类型的一些信息。因此,实现相等运算符可能不可行。

我可能错了,但我认为
std::function
对象的相等性在一般意义上是不可解决的。例如:

#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <cstdio>

void f() {
    printf("hello\n");
}

int main() {
    boost::function<void()> f1 = f;
    boost::function<void()> f2 = boost::bind(f);

    f1();
    f2();
}
#包括
#包括
#包括
void f(){
printf(“hello\n”);
}
int main(){
boost::函数f1=f;
boost::function f2=boost::bind(f);
f1();
f2();
}
f1
f2
是否相等?如果我添加任意数量的函数对象,它们只是以各种方式相互包装,最终归结为对
f
的调用,会怎么样。。。还平等吗

为什么平等不具有可比性

std::function
是任意可调用类型的包装器,因此为了实现相等比较,您必须要求所有可调用类型都是相等可比较的,这给任何实现函数对象的人带来了负担。即使这样,您也会得到一个狭隘的相等概念,因为如果(例如)等价函数是由不同顺序的绑定参数构造的,那么它们会比较不相等。我相信在一般情况下是不可能检验等价性的

“类型系统中可能存在的漏洞”是什么

我想这意味着删除操作符,并且肯定地知道使用它们永远不会给出有效的代码,比证明在一些以前未发现的情况下不存在不必要的隐式转换更容易

它与其他的有什么不同


std::shared_ptr
具有定义良好的平等语义;当且仅当两个指针均为空或均为非空且指向同一对象时,这两个指针才相等。

至少可以做到的是,如果std::function保存用于绑定到字符串的函数的地址,并改为使用字符串比较

为什么
std::function
不具有可比性

我认为主要原因是,如果是,那么它就不能用于非相等可比类型,即使从未执行相等比较

也就是说,执行比较的代码应该尽早实例化——在可调用对象存储到
std::function
中时,例如在一个构造函数或赋值运算符中

这样的限制将极大地缩小应用范围,对于“通用多态函数包装器”来说显然是不可接受的


需要注意的是,可以使用可调用对象(但不能使用另一个
boost::function

函数对象包装器可以通过
==
进行比较=代码

这是可能的,因为函数
f1 == f2;
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <cstdio>

void f() {
    printf("hello\n");
}

int main() {
    boost::function<void()> f1 = f;
    boost::function<void()> f2 = boost::bind(f);

    f1();
    f2();
}
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);
    }
};
void register_callback(std::function<function_signature> callback);
void unregister_callback(function_comparable<function_signature> callback);
//             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
________________
template <class Function>
struct Comparator
{
    bool operator()(const Function& f1, const Function& f2) const
    {
        auto ptr1 = f1.target<Function>();
        auto ptr2 = f2.target<Function>();
        return ptr1 < ptr2;
    }
};

typedef function<void(void)> Function;

set<Function, Comparator<Function>> setOfFunc;

void f11() {}

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "was inserted - " << setOfFunc.insert(bind(&f11)).second << endl;  // 1 - inserted
    cout << "was inserted - " << setOfFunc.insert(f11).second << endl;         // 0 - not inserted
    cout << "# of deleted is " << setOfFunc.erase(f11) << endl;

    return 0;
}
if (std::is_same<T1, T2>::value)
{
    ...
}