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

相关文章

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java MQTT实战应用

《JavaMQTT实战应用》本文详解MQTT协议,涵盖其发布/订阅机制、低功耗高效特性、三种服务质量等级(QoS0/1/2),以及客户端、代理、主题的核心概念,最后提供Linux部署教程、Sprin... 目录一、MQTT协议二、MQTT优点三、三种服务质量等级四、客户端、代理、主题1. 客户端(Clien