C++函数对象-部分函数应用-表明一个对象是标准占位符,或者可以用作标准占位符(std::is_placeholder)

本文主要是介绍C++函数对象-部分函数应用-表明一个对象是标准占位符,或者可以用作标准占位符(std::is_placeholder),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

任何定义了函数调用操作符的对象都是函数对象。C++ 支持创建、操作新的函数对象,同时也提供了许多内置的函数对象。

部分函数应用

std::bind_front 与 std::bind 提供部分函数应用的支持,即绑定参数到函数以创建新函数。

表明一个对象是标准占位符,或者可以用作标准占位符

std::is_placeholder

template< class T >
struct is_placeholder;

(C++11 起)

T 是标准占位符( _1 、 _2 、 _3 ……)的类型,则此模板分别派生自 std::integral_constant<int,1> 、 std::integral_constant<int,2> 、 std::integral_constant<int,3> 。

T 不是标准占位符类型,则此模板派生自 std::integral_constant<int,0> 。

可以为任何用户定义 T 类型特化模板:特化必须满足一元类型特征 (UnaryTypeTrait) ,拥有 std::integral_constant<int, N> 而 N > 0 指示 T 应被处理成第 N 个占位符类型的基特征 (BaseCharacteristic)

std::bind 用 std::is_placeholder 检测未绑定参数的占位符。

辅助变量模板

template< class T >

 

继承自 std::integral_constant

成员常量

value

[静态]

占位符值,或对于非占位符类型为 0
(公开静态成员常量)

成员函数

operator int

转换对象为 int ,返回 value
(公开成员函数)

operator()

(C++14)

返回 value
(公开成员函数)

成员类型

类型定义
value_typeint
typestd::integral_constant<int, value>

调用示例 

#include <iostream>
#include <type_traits>
#include <functional>struct Cell
{int x;int y;Cell() = default;Cell(int a, int b): x(a), y(b) {}Cell(const Cell &cell){x = cell.x;y = cell.y;}bool operator <(const Cell &cell) const{if (x == cell.x){return y < cell.y;}else{return x < cell.x;}}Cell &operator+(const Cell &cell){x += cell.x;y += cell.y;return *this;}
};std::ostream &operator<<(std::ostream &os, const Cell &cell)
{os << "{" << cell.x << "," << cell.y << "}";return os;
}struct MyBind1
{
};struct MyBind2
{
};namespace std
{
//第二个参数
template<>
struct is_placeholder<MyBind1> : public integral_constant<int, 1> {};
//第二个参数
template<>
struct is_placeholder<MyBind2> : public integral_constant<int, 2> {};
}Cell Function1(Cell cell1, Cell cell2)
{return cell1 + cell2;
}int main()
{std::cout << "Standard placeholder _1 is for the argument number: "<< std::is_placeholder<decltype(std::placeholders::_1)>::value<< std::endl;std::cout << "Standard placeholder _2 is for the argument number: "<< std::is_placeholder<decltype(std::placeholders::_2)>::value<< std::endl;std::cout << "Standard placeholder _5 is for the argument number: "<< std::is_placeholder<decltype(std::placeholders::_5)>::value<< std::endl;std::cout << "Standard placeholder _16 is for the argument number: "<< std::is_placeholder<decltype(std::placeholders::_16)>::value<< std::endl;std::cout << "Standard placeholder _17 is for the argument number: "<< std::is_placeholder<decltype(std::placeholders::_17)>::value<< std::endl;// 取第一个参数MyBind1 myBind1;auto function1 = std::bind(Function1, myBind1, Cell{108, 108});std::cout << "Adding Cell{108, 108} to Cell{106, 106} selected with a custom placeholder gives "<< function1(Cell{106, 106}, Cell{107, 107}) << std::endl;// 取第二个参数MyBind2 myBind2;auto function2 = std::bind(Function1, myBind2, Cell{108, 108});std::cout << "Adding Cell{108, 108} to Cell{107, 107} selected with a custom placeholder gives "<< function2(Cell{106, 106}, Cell{107, 107}) << std::endl;return 0;
}

输出

Standard placeholder _1 is for the argument number: 1
Standard placeholder _2 is for the argument number: 2
Standard placeholder _5 is for the argument number: 5
Standard placeholder _16 is for the argument number: 16
Standard placeholder _17 is for the argument number: 17
Adding Cell{108, 108} to Cell{106, 106} selected with a custom placeholder gives {214,214}
Adding Cell{108, 108} to Cell{107, 107} selected with a custom placeholder gives {215,215}

这篇关于C++函数对象-部分函数应用-表明一个对象是标准占位符,或者可以用作标准占位符(std::is_placeholder)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/639308

相关文章

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

MySQL JSON 查询中的对象与数组技巧及查询示例

《MySQLJSON查询中的对象与数组技巧及查询示例》MySQL中JSON对象和JSON数组查询的详细介绍及带有WHERE条件的查询示例,本文给大家介绍的非常详细,mysqljson查询示例相关知... 目录jsON 对象查询1. JSON_CONTAINS2. JSON_EXTRACT3. JSON_TA

C++作用域和标识符查找规则详解

《C++作用域和标识符查找规则详解》在C++中,作用域(Scope)和标识符查找(IdentifierLookup)是理解代码行为的重要概念,本文将详细介绍这些规则,并通过实例来说明它们的工作原理,需... 目录作用域标识符查找规则1. 普通查找(Ordinary Lookup)2. 限定查找(Qualif

C#之List集合去重复对象的实现方法

《C#之List集合去重复对象的实现方法》:本文主要介绍C#之List集合去重复对象的实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C# List集合去重复对象方法1、测试数据2、测试数据3、知识点补充总结C# List集合去重复对象方法1、测试数据