【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最 ...
随机推荐
- scala成长之路(5)问题记录
还是在看scala sdk源码的时候,有很多问题要考自己慢慢摸索,这里做个记录. 一. 隐式转换的作用域? 隐式转换需要三个因素 1. 己方(当前对象) 2. 转换函数 3. 对方(转换的目标类) 这 ...
- 谭浩强第四版第九章课后习题12>>>建立一个链表,每个节点包括:学号、姓名、性别、年龄。输入一个年龄,若链表 中的结点所包含的年龄等于此年龄,则删除此结点。
#include<stdio.h> #include<stdlib.h> #define N sizeof(link) typedef struct lin { struct ...
- 深入了解jQuery Mobile-1
介绍 jQuery Mobile是一种触控优化的HTML5 UI框架,旨在制作可在所有智能手机,平板电脑和台式机设备上访问的响应式网站和应用程序 移动页面结构 jQuery Mobile站点必须以HT ...
- python爬取豌豆荚中的详细信息并存储到SQL Server中
买了本书<精通Python网络爬虫>,看完了第6章,我感觉我好像可以干点什么:学的不多,其中的笔记我放到了GitHub上:https://github.com/NSGUF/PythonLe ...
- 高德API+.NET解决租房问题(可能是最可靠房源:上海互助租房)
作者:李国宝链接:https://zhuanlan.zhihu.com/p/22113421来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. PS:最近点赞和关注的小伙伴 ...
- 学习SQLite基本语句
SQLite 是一个开源的嵌入式关系数据库,实现自包容.零配置.支持事务的SQL数据库引擎. 其特点是高度便携.使用方便.结构紧凑.高效.可靠. 与其他数据库管理系统不同,SQLite 的安装和运行非 ...
- android开源项目之OTTO事件总线(二)官方demo解说
官方demo见 https://github.com/square/otto 注意自己该编译版本为2.3以上,默认的1.6不支持match_parent属性,导致布局文件出错. 另外需要手动添加an ...
- Android TextView 单行文本的坑
这是android系统的一个bug,描述如下:https://code.google.com/p/android/issues/detail?id=33868 具体来说就是当一个TextView设置了 ...
- javac、jar使用实录
因项目管理部署需要,记录一下过程,以免下次忘记了,再次使用又需要重头再来,只记录正确的操作方式,可能会提到某些错误 建立项目所在目录F:\www 案例一 其下建立项目的java源文件的包目录结构.ja ...
- Promise关键知识
异步是ES6中使用频率最高的特性之一,也是面试中经常会被问到的问题,特此整理了相应的笔记 一.Promise的三种状态 pending-异步操作没有结束 fulfilled-异步操作已成功结束,最常见 ...