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

相关文章

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

Java对象转换的实现方式汇总

《Java对象转换的实现方式汇总》:本文主要介绍Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java对象转换的多种实现方式1. 手动映射(Manual Mapping)2. Builder模式3. 工具类辅助映

C语言中位操作的实际应用举例

《C语言中位操作的实际应用举例》:本文主要介绍C语言中位操作的实际应用,总结了位操作的使用场景,并指出了需要注意的问题,如可读性、平台依赖性和溢出风险,文中通过代码介绍的非常详细,需要的朋友可以参... 目录1. 嵌入式系统与硬件寄存器操作2. 网络协议解析3. 图像处理与颜色编码4. 高效处理布尔标志集合

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Java中的Lambda表达式及其应用小结

《Java中的Lambda表达式及其应用小结》Java中的Lambda表达式是一项极具创新性的特性,它使得Java代码更加简洁和高效,尤其是在集合操作和并行处理方面,:本文主要介绍Java中的La... 目录前言1. 什么是Lambda表达式?2. Lambda表达式的基本语法例子1:最简单的Lambda表

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

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 子字