Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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用作std::not2的参数?_C++_Templates_C++17_Typedef - Fatal编程技术网

C++ 为什么std::function用作std::not2的参数?

C++ 为什么std::function用作std::not2的参数?,c++,templates,c++17,typedef,C++,Templates,C++17,Typedef,std::not2的实现方式如下: template <class Predicate> binary_negate<Predicate> not2 (const Predicate& pred) { return binary_negate<Predicate>(pred); } template <class _Predicate> class binary_negate : public binary_function<

std::not2的实现方式如下:

template <class Predicate>
binary_negate<Predicate> not2 (const Predicate& pred)
{
    return binary_negate<Predicate>(pred);
}
template <class _Predicate>
class binary_negate : public binary_function<typename _Predicate::first_argument_type,
                                             typename _Predicate::second_argument_type,
                                             bool>
{
    // ...
}
模板
二进制否定not2(常量谓词和pred)
{
返回二进制否定(pred);
}
它在其实现中使用std::binary_negate,从std::binary_函数继承的binary_negate在其模板参数中需要_谓词::第一个_参数_类型和_谓词::第二个_参数_类型,方式如下:

template <class Predicate>
binary_negate<Predicate> not2 (const Predicate& pred)
{
    return binary_negate<Predicate>(pred);
}
template <class _Predicate>
class binary_negate : public binary_function<typename _Predicate::first_argument_type,
                                             typename _Predicate::second_argument_type,
                                             bool>
{
    // ...
}
模板
类binary_negate:公共binary_函数
{
// ...
}
我的问题是,如果我传递的谓词中没有第一个参数类型和第二个参数类型的typedef,为什么我可以这样写

struct new_same
{
    bool operator()(int a, int b) const { return a == b; }
};
auto not_same = std::not2(std::function<bool(int, int)>(new_same()));
struct new\u相同
{
bool运算符()(inta,intb)常量{返回a==b;}
};
auto not_same=std::not2(std::function(new_same());
还有一个(来自):

如果我传递的谓词中没有第一个参数类型和第二个参数类型的typedef,为什么我可以这样写

struct new_same
{
    bool operator()(int a, int b) const { return a == b; }
};
auto not_same = std::not2(std::function<bool(int, int)>(new_same()));
typedef在那里
binary_negate
可以从
谓词
(在您的例子中是
std::function
)获取它们

所有这些都在
C++17
中被弃用,并在
C++20
中删除。我必须承认,我不能告诉你替换的是什么。

根据:

not2
是一个帮助函数,用于创建返回传递的二进制谓词函数的补码的函数对象。创建的函数对象的类型为
std::binary\u negate

以及:

二进制谓词类型必须定义两个成员类型,
第一个参数类型
第二个参数类型
,它们可以转换为谓词的参数类型[…]
std::function
或从另一个调用
std::not2
获得的函数对象定义了这些类型,从不推荐的
std::binary_函数
派生的函数对象也定义了这些类型

在我的实现中(使用GCC 9.2.0进行MinGW),
std::function
派生自
\u可能是一元函数或二元函数
,当用两个参数实例化时,它派生自
std::binary\u function



请注意,此功能已弃用。不幸的是,到目前为止,我还找不到它的替代品。

如果您查看std::function实现,您将看到它继承了一个名为“可能是一元函数”或“二进制函数”的类。如果参数数为2,此继承将导致std::函数继承std::binary_函数

  template<typename _Res, typename... _ArgTypes>
    class function<_Res(_ArgTypes...)>
    : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
      private _Function_base
    {
        ...
    }
模板
类函数
:public _可能是一元函数或二元函数,
专用功能库
{
...
}
模板
结构可以是一元函数或二元函数{};
///根据需要从@c一元函数派生。
模板
结构可能是一元函数或二元函数
:std::一元函数{};
///根据需要从@c二进制函数派生。
模板
结构可能是一元函数或二元函数
:std::二进制函数{};
通过从std::binary_函数继承,第一个_参数_类型和第二个_参数_类型被添加到std::函数:

template<typename _Arg1, typename _Arg2, typename _Result>
 struct binary_function
    {
      /// @c first_argument_type is the type of the first argument
      typedef _Arg1     first_argument_type; 

      /// @c second_argument_type is the type of the second argument
      typedef _Arg2     second_argument_type;

      /// @c result_type is the return type
      typedef _Result   result_type;
    };
模板
结构二进制函数
{
///@c first_argument_type是第一个参数的类型
typedef_Arg1第一个参数_类型;
///@c second_argument_type是第二个参数的类型
typedef_Arg2第二个参数_类型;
///@c result_type是返回类型
typedef_Result_type;
};

谢谢,我在头文件中没有找到这些typedef,但它们存在于其他地方。至少我看到它们存在于cppreference.com文档中。@Oleg cppreference通常非常准确。如果我想了解更多细节,我会先查看标准,而不是浏览实现。很容易将实现细节与指定的实际内容混淆。考虑到不赞成和反对的动机(例如,没有重放),请参阅。