【C++ troubleshooting】A case about decltype
template <typename iter_t>
bool next_permutation(iter_t beg, iter_t end) {
//
if (beg == end || beg + 1 == end) {
return false;
}
//在白板上写代码别忘了加 }
for (int *i = end - 2; i >= beg; --i) {
auto iter = std::lower_bound(i + 1, end, *i, std::greater<decltype(*beg)>{});
if (iter != i + 1) {
std::swap(*(iter - 1), *i);
std::reverse(i + 1, end);
return true;
}
}
return false;
}
ptr_t ptr;
*ptr;
As we know, in C++, for a variable ptr of a pointer (or iterator) type, the expression *ptr returns a reference to the object that ptr points to.
Suppose the object that ptr points to is of type T, then decltype(*ptr) will yield the type T&.
So, in the above code, the line
auto iter = std::lower_bound(i + 1, end, *i, std::greater<decltype(*beg)>{});
becomes
auto iter = std::lower_bound(i + 1, end, *i, std::greater<int&>{});
when the function template next_permutation is instantiated.
However, such an instantiation of std::lower_bound will not compile.
Let's see the compile error:
In file
.../include/c++/8.3.0/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_comp_val::operator()(_Iterator, _Value&) [with _Iterator = int*; _Value = const int; _Compare = std::greater<int&>]':
.../include/c++/8.3.0/bits/predefined_ops.h:177:11: error: no match for call to '(std::greater<int&>) (int&, const int&)'
{ return bool(_M_comp(*__it, __val)); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~ In file
.../include/c++/8.3.0/bits/predefined_ops.h:177:11:error: binding reference of type 'int&' to 'const int' discards qualifiers
{ return bool(_M_comp(*__it, __val)); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~
将其中提到的函数片段,还原如下
template<typename _Compare>
struct _Iter_comp_val
{
_Compare _M_comp;
// constructors
template<typename _Iterator, typename _Value>
bool
operator()(_Iterator __it, _Value& __val)
{ return bool(_M_comp(*__it, __val)); }
};
这个问题我还没搞懂。
【C++ troubleshooting】A case about decltype的更多相关文章
- 【原创翻译】The Case for the Reduced Instruction Set Computer
RISC机的例子 David A. Patterson 加州大学伯克利分校计算机科学系 David R. Ditzel 贝尔实验室计算科学研究中心 介绍 计算机体系结构最主要的目标之一就是设计比之前产 ...
- 【35.37%】【codeforces 556C】Case of Matryoshkas
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【66.47%】【codeforces 556B】Case of Fake Numbers
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【52.49%】【codeforces 556A】Case of the Zeros and Ones
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【深入浅出jQuery】源码浅析--整体架构
最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...
- 【知识必备】RxJava+Retrofit二次封装最佳结合体验,打造懒人封装框架~
一.写在前面 相信各位看官对retrofit和rxjava已经耳熟能详了,最近一直在学习retrofit+rxjava的各种封装姿势,也结合自己的理解,一步一步的做起来. 骚年,如果你还没有掌握ret ...
- 搭建一套自己实用的.net架构(3)【ORM-Dapper+DapperExtensions】
现在成熟的ORM比比皆是,这里只介绍Dapper的使用(最起码我在使用它,已经运用到项目中,小伙伴们反馈还可以). 优点: 1.开源.轻量.小巧.上手容易. 2.支持的数据库还蛮多的, Mysql,S ...
- MongoDB【第一篇】MongodDB初识
NoSQL介绍 一.NoSQL简介 NoSQL,全称是”Not Only Sql”,指的是非关系型的数据库. 非关系型数据库主要有这些特点:非关系型的.分布式的.开源的.水平可扩展的. 原始的目的是为 ...
- ASP.NET Core中的依赖注入(5): ServiceProvider实现揭秘 【解读ServiceCallSite 】
通过上一篇的介绍我们应该对实现在ServiceProvider的总体设计有了一个大致的了解,但是我们刻意回避一个重要的话题,即服务实例最终究竟是采用何种方式提供出来的.ServiceProvider最 ...
随机推荐
- docker API 配置与使用
在网上看到一大堆乱乱七八招的博客,很多都不能用,我根据这些天踩的坑来总结一下吧 首先!怎么配置 docker API 两种方法 在/etc/sysconfig/docker文件里加一行OPTIONS= ...
- PHPPCRE正则解析
一.前言 前面的博客里,有对字符集的解析.这里就不是字符集的事儿了,在PHP中很多函数的处理默认是unicode中的UTF-8编码格式.那么废话不多说,直接开始正题. 二.PHP函数mb_split解 ...
- 洛谷 T51922 父子
题目描述 对于全国各大大学的男生寝室,总是有各种混乱的父子关系. 那么假设现在我们一个男生寝室有不同的 nn 个人,每个人都至多有一个“爸爸”,可以有多个“儿子”,且有且只有一个人没有“爸爸”(毕竟是 ...
- MyBatis实现拦截器分页功能
1.原理 在mybatis使用拦截器(interceptor),截获所执行方法的sql语句与参数. (1)修改sql的查询结果:将原sql改为查询count(*) 也就是条数 (2)将语句sql进行拼 ...
- COURSES POJ1469(模板)
Description Consider a group of N students and P courses. Each student visits zero, one or more than ...
- CDH,CM下载
wget -c -r -nd -np -k -L -A rpm http://archive-primary.cloudera.com/cdh5/parcels/latest/ http://arch ...
- java中array,arrayList,iterator;
Array String []a = new String[10] ; a[0] = "test" ; String []a = new String[]{&quo ...
- Linux 下 PHP 扩展 PDO 编译安装
1.进入 PHP 的软件包 pdo 扩展目录中(注:不是 PHP 安装目录) [root@tester /]# /home/tdweb/php-5.4.34/ext/pdo_mysql 执行 phpi ...
- HMM相关文章索引
HMM相关文章索引 1条回复 HMM系列文章是52nlp上访问量较高的一批文章,这里做个索引,方便大家参考. HMM学习 HMM学习最佳范例一:介绍 HMM学习最佳范例二:生成模式 HMM学习最佳范例 ...
- php用GD库给图片添加水印
php用GD库给图片添加文字水印,整个代码比较简单,DEMO如下: <?php /*打开图片*/ //1.配置图片路径 $src = "aeroplane.jpg"; //2 ...