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

相关文章

Python结合PyWebView库打造跨平台桌面应用

《Python结合PyWebView库打造跨平台桌面应用》随着Web技术的发展,将HTML/CSS/JavaScript与Python结合构建桌面应用成为可能,本文将系统讲解如何使用PyWebView... 目录一、技术原理与优势分析1.1 架构原理1.2 核心优势二、开发环境搭建2.1 安装依赖2.2 验

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

一文详解如何在Python中从字符串中提取部分内容

《一文详解如何在Python中从字符串中提取部分内容》:本文主要介绍如何在Python中从字符串中提取部分内容的相关资料,包括使用正则表达式、Pyparsing库、AST(抽象语法树)、字符串操作... 目录前言解决方案方法一:使用正则表达式方法二:使用 Pyparsing方法三:使用 AST方法四:使用字

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

SpringShell命令行之交互式Shell应用开发方式

《SpringShell命令行之交互式Shell应用开发方式》本文将深入探讨SpringShell的核心特性、实现方式及应用场景,帮助开发者掌握这一强大工具,具有很好的参考价值,希望对大家有所帮助,如... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定

SpringBoot应用中出现的Full GC问题的场景与解决

《SpringBoot应用中出现的FullGC问题的场景与解决》这篇文章主要为大家详细介绍了SpringBoot应用中出现的FullGC问题的场景与解决方法,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录Full GC的原理与触发条件原理触发条件对Spring Boot应用的影响示例代码优化建议结论F

MySQL 分区与分库分表策略应用小结

《MySQL分区与分库分表策略应用小结》在大数据量、复杂查询和高并发的应用场景下,单一数据库往往难以满足性能和扩展性的要求,本文将详细介绍这两种策略的基本概念、实现方法及优缺点,并通过实际案例展示如... 目录mysql 分区与分库分表策略1. 数据库水平拆分的背景2. MySQL 分区策略2.1 分区概念