【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最 ...
随机推荐
- 转:java中的定时任务
引自:http://www.cnblogs.com/wenbronk/p/6433178.html java中的定时任务, 使用java实现有3种方式: 1, 使用普通thread实现 @Test p ...
- Git推送到远程分支出错
执行git push -u origin master fatal: 'git@github.com:qilinonline/git_test.git' does not appear to be a ...
- 让图片在div盒子中水平垂直居中
//调整多张图片,让图片水平垂直居中 function adjustImg(){ let imgDiv = document.getElementsByClassName("img" ...
- jquer搜索
<body> <br/> <center> <tr><td>创建时间:</td><td><input type ...
- Jackson 触发的String.intern() bug, 导致内存持续增加,JVM-Java内存泄漏
我在本地用Jackson可以复现这个问题了. import java.io.IOException; import java.util.Map; import java.util.Random; im ...
- queue消息队列
class queue.Queue(maxsize=0) #先入先出 class queue.LifoQueue(maxsize=0) #last in fisrt out class queue. ...
- Scrapy进阶
当我们使用scrapy框架爬取网站的时候,我们会有一个入口的url,一个名为start_urls,我们爬取的第一个网页是从这一开始的. 需求: 现在我们有一个这样的需求,比如说我们对起始的URL有一个 ...
- Leecode刷题之旅-C语言/python-58.最后一个单词的长度
/* * @lc app=leetcode.cn id=58 lang=c * * [58] 最后一个单词的长度 * * https://leetcode-cn.com/problems/length ...
- WPF DateTimePicker 和 TimeSpanPicker 控件发布
原文:WPF DateTimePicker 和 TimeSpanPicker 控件发布 根据http://datetimepickerwpf.codeplex.com/ 这个项目重构了一下代码设计了我 ...
- 7.Mongodb复制(副本集)
1.复制 什么是复制 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性,并可以保证数据的安全性 复制还允许从硬件故障和服务中断中恢复数据 为什么要复制 数据备份 数据灾难恢复 ...