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的更多相关文章

  1. 【原创翻译】The Case for the Reduced Instruction Set Computer

    RISC机的例子 David A. Patterson 加州大学伯克利分校计算机科学系 David R. Ditzel 贝尔实验室计算科学研究中心 介绍 计算机体系结构最主要的目标之一就是设计比之前产 ...

  2. 【35.37%】【codeforces 556C】Case of Matryoshkas

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【66.47%】【codeforces 556B】Case of Fake Numbers

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【52.49%】【codeforces 556A】Case of the Zeros and Ones

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【深入浅出jQuery】源码浅析--整体架构

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...

  6. 【知识必备】RxJava+Retrofit二次封装最佳结合体验,打造懒人封装框架~

    一.写在前面 相信各位看官对retrofit和rxjava已经耳熟能详了,最近一直在学习retrofit+rxjava的各种封装姿势,也结合自己的理解,一步一步的做起来. 骚年,如果你还没有掌握ret ...

  7. 搭建一套自己实用的.net架构(3)【ORM-Dapper+DapperExtensions】

    现在成熟的ORM比比皆是,这里只介绍Dapper的使用(最起码我在使用它,已经运用到项目中,小伙伴们反馈还可以). 优点: 1.开源.轻量.小巧.上手容易. 2.支持的数据库还蛮多的, Mysql,S ...

  8. MongoDB【第一篇】MongodDB初识

    NoSQL介绍 一.NoSQL简介 NoSQL,全称是”Not Only Sql”,指的是非关系型的数据库. 非关系型数据库主要有这些特点:非关系型的.分布式的.开源的.水平可扩展的. 原始的目的是为 ...

  9. ASP.NET Core中的依赖注入(5): ServiceProvider实现揭秘 【解读ServiceCallSite 】

    通过上一篇的介绍我们应该对实现在ServiceProvider的总体设计有了一个大致的了解,但是我们刻意回避一个重要的话题,即服务实例最终究竟是采用何种方式提供出来的.ServiceProvider最 ...

随机推荐

  1. 14.2 multiprocessing--多线程

    本模块提供了多进程进行共同协同工作的功能.由于Python存在GIL锁,对于多线程来说,这只是部分代码可以使用多CPU的优势,对于想全部使用多CPU的性能,让每一个任务都充分地使用CPU,那么使用多进 ...

  2. Mysql基础3-数据操作语言DML-数据查询语言DQL

    主要: 数据操作语言DML 数据查询语言DQL 数据操作语言DML DML: Data Mutipulation Language 插入数据(增) 一般插入数据形式 1)形式1: insert [in ...

  3. Hadoop参数调优

    转自:http://blog.sina.com.cn/s/blog_6a67b5c50100vop9.html dfs.block.size 决定HDFS文件block数量的多少(文件个数),它会间接 ...

  4. Python学习 :六个标准数据类型

    一.Numbers(数字类型) 数字类型主要分为两种—— 整数(Integer)与 浮点数(Float) 整数分为整型和长整型(在Python3中已经不再区分为整型与长整型,统一称为整型) 注意:数字 ...

  5. 10-C++远征之模板篇-学习笔记

    C++远征之模板篇 将会学到的内容: 模板函数 & 模板类 -> 标准模板类 友元函数 & 友元类 静态数据成员 & 静态成员函数 运算符重载: 一切皆有可能 友元函数 ...

  6. office 总结

    wps word中双击格式刷即可开启永久格式刷

  7. c++ map的使用方法

    1.头文件:#include<map> 2.定义:map<typename1,typename2> mp 注:字符串数组只能用string而不能使用char[] 3.访问方式: ...

  8. dfs Gym - 100989L

    AbdelKader enjoys math. He feels very frustrated whenever he sees an incorrect equation and so he tr ...

  9. oracle 数据被修改怎么修复?(闪回)

    数据被删除 或者 update 的时候忘记勾选where 限制条件,数据全部更新了?  怎么办? 要跑路了? NO !!! 看下面,迅速帮你闪回数据! demo sql: 1. SELECT * FR ...

  10. Java:位移运算符

    Java中有三个位移运算符,用于对int类型整数的二进制补码进行操作: 1. "<<": 左移运算符 在二进制补码末尾添加“0”,之前的其他位相当于左移了一位,可看作成 ...