Ubuntu16.04 ARM 编译 编译器版本和unordered_map map问题
源文件内使用unordered_map时候,例如如下demo
#include <unordered_map> void foo(const std::unordered_map<int,int> &) {}
int main()
{
foo({});
}
GCC版本大于或者等于4.9,会报如下错误
map2.cpp:7:19: error: converting to ‘const std::unordered_map<int, int>’ from initializer list would use explicit constructor ‘std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unordered_map(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type, const hasher&, const key_equal&, const allocator_type&) [with _Key = int; _Tp = int; _Hash = std::hash<int>; _Pred = std::equal_to<int>; _Alloc = std::allocator<std::pair<const int, int> >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type = long unsigned int; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hasher = std::hash<int>; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_equal = std::equal_to<int>; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::allocator_type = std::allocator<std::pair<const int, int> >]’
其实就是 Implicit conversion failure from initializer list
如下测试来自 :https://stackoverflow.com/questions/26947704/implicit-conversion-failure-from-initializer-list
Testing with other compiler/library implementations: GCC < 4.9 accepts this without complaining,
Clang 3.5 with libstdc++ fails with a similar message,
Clang 3.5 with libc++ accepts this,
ICC 15.something accepts this (not sure which standard library it is using).
A couple of more baffling points: replacing std::unordered_map with std::map makes the error go away,
replacing foo({}) with foo foo({{}}) also makes the error go away.
Also, replacing {} with a non-empty initializer list works as expected in all cases.
实际编译过程中,我将unordered_map替换成了map,不影响适应也不会报错
来自stackover的答案解析
The indirect initialization syntax with a braced-init-list your code is using is called copy-list-initialization.
The overload resolution procedure selecting the best viable constructor for that case is described in the following section of the C++ Standard:
§ 13.3.1.7 Initialization by list-initialization [over.match.list]
When objects of non-aggregate class type T are list-initialized (8.5.4), overload resolution selects the constructor in two phases:
— Initially, the candidate functions are the initializer-list constructors (8.5.4) of the class T and the argument list consists of the initializer list as a single argument.
— If no viable initializer-list constructor is found, overload resolution is performed again, where the candidate functions are all the constructors of the class T and the
argument list consists of the elements of the initializer list.
If the initializer list has no elements and T has a default constructor, the first phase is omitted. In copy-list-initialization, if an explicit constructor is chosen, the
initialization is ill-formed. [ Note: This differs from other situations (13.3.1.3, 13.3.1.4), where only converting constructors are considered for copy-initialization.
This restriction only applies if this initialization is part of the final result of overload resolution. — end note ].
According to that, an initializer-list-constructor (the one callable with a single argument matching the constructor's parameter of type std::initializer_list<T>
) is usually preferred to other constructors, but not if a default-constructor is available, and the braced-init-list used for list-initialization is empty.
What is important here, the set of constructors of the standard library's containers has changed between C++11 and C++14 due to LWG issue 2193. In case of std::unordered_map
, for the sake of our analysis, we are interested in the following difference:
C++11:
explicit unordered_map(size_type n = /* impl-defined */,
const hasher& hf = hasher(),
const key_equal& eql = key_equal(),
const allocator_type& alloc = allocator_type()); unordered_map(initializer_list<value_type> il,
size_type n = /* impl-defined */,
const hasher& hf = hasher(),
const key_equal& eql = key_equal(),
const allocator_type& alloc = allocator_type());
C++14:
unordered_map(); explicit unordered_map(size_type n,
const hasher& hf = hasher(),
const key_equal& eql = key_equal(),
const allocator_type& alloc = allocator_type()); unordered_map(initializer_list<value_type> il,
size_type n = /* impl-defined */,
const hasher& hf = hasher(),
const key_equal& eql = key_equal(),
const allocator_type& alloc = allocator_type());
In other words, there is a different default constructor (the one that can be called without arguments) depending on the language standard (C++11/C++14), and, what is crucial, the default constructor in C++14 is now made non-explicit
.
That change was introduced so that one can say:
std::unordered_map<int,int> m = {};
or:
std::unordered_map<int,int> foo()
{
return {};
}
which are both semantically equivalent to your code (passing {}
as the argument of a function call to initialize std::unordered_map<int,int>
).
That is, in case of a C++11-conforming library, the error is expected, as the selected (default) constructor is explicit
, therefore the code is ill-formed:
explicit unordered_map(size_type n = /* impl-defined */,
const hasher& hf = hasher(),
const key_equal& eql = key_equal(),
const allocator_type& alloc = allocator_type());
In case of a C++14-conforming library, the error is not expected, as the selected (default) constructor is not explicit
, and the code is well-formed:
unordered_map();
As such, the different behavior you encounter is solely related to the version of libstdc++ and libc++ you are using with different compilers/compiler options.
Replacing std::unordered_map
with std::map
makes the error go away. Why?
I suspect it's just because std::map
in the libstdc++ version you are using was already updated for C++14.
Replacing foo({})
with foo({{}})
also makes the error go away. Why?
Because now this is copy-list-initialization {{}}
with a non-empty braced-init-list (that is, it has one element inside, initialized with an empty braced-init-list {}
), so the rule from the first phase of § 13.3.1.7 [over.match.list]/p1 (quoted before) that prefers an initializer-list-constructor to other ones is applied. That constructor is not explicit
, hence the call is well-formed.
Replacing {}
with a non-empty initializer list works as expected in all cases. Why?
Same as above, the overload resolution ends up with the first phase of § 13.3.1.7 [over.match.list]/p1.
Ubuntu16.04 ARM 编译 编译器版本和unordered_map map问题的更多相关文章
- Ubuntu16.04下编译安装OpenCV3.4.0(C++ & python)
Ubuntu16.04下编译安装OpenCV3.4.0(C++ & python) 前提是已经安装了python2,python3 1)安装各种依赖库 sudo apt-get update ...
- 在ubuntu16.04上编译android源码【转】
本文转载自:http://blog.csdn.net/fuchaosz/article/details/51487585 1 前言 经过3天奋战,终于在Ubuntu 16.04上把Android 6. ...
- Ubuntu16.04系统中不同版本Python之间的转换
Ubuntu系统自带的版本是2.7.12 安装好python3.6之后,改变一下Python的优先级(需要root权限). 在使用下面这个命令查看电脑里面有几个Python版本 update-alte ...
- Ubuntu16.04下编译OpenCV2.4.13静态库(.a文件)
Ubuntu16.04下编译OpenCV2.4.13静态库(.a文件) https://blog.csdn.net/woainishifu/article/details/79712110 我们在做项 ...
- Ubuntu16.04下安装多版本cuda和cudnn
Ubuntu16.04下安装多版本cuda和cudnn 原文 https://blog.csdn.net/tunhuzhuang1836/article/details/79545625 前言 因为之 ...
- ubuntu16.04下编译ceres-solver
一.编译环境 ubuntu16.04 二.准备工作之安装必要的库 2.1安装cmake sudo apt-get install cmake 2.2 安装google-glog + gflags su ...
- ubuntu16.04下编译安装vim8.1
之前写过一篇centos7下编译安装vim8.0的教程,ubuntu16.04相比centos7下安装过程不同在于依赖包名字的不同,其余都是一样.下面给出ubuntu16.04编译安装vim8.0需要 ...
- Ubuntu16.04下编译安装及运行单目ORBSLAM2
官网有源代码和配置教程,地址是 https://github.com/raulmur/ORB_SLAM2 1 安装必要工具 首先,有两个工具是需要提前安装的.即cmake和Git. sudo apt- ...
- ubuntu16.04安装Ros(kinetic版本)【亲测好用】
准备 1.ubuntu16.04 64位桌面版 ps:关于系统的下载和安装这里不做介绍,请自行百度,不是介绍重点 2.更改源 图上的几个勾默认是选上的,如果没有选上,选成上图这样(如果修改过勾,点击关 ...
随机推荐
- 使用OpenCV对图像进行缩放
OpenCV:图片缩放和图像金字塔 对图像进行缩放的最简单方法当然是调用resize函数啦! resize函数可以将源图像精确地转化为指定尺寸的目标图像. 要缩小图像,一般推荐使用CV_INETR_A ...
- 排序算法总结(C#版)
算法质量的衡量标准: 1:时间复杂度:分析关键字比较次数和记录的移动次数: 2:空间复杂度:需要的辅助内存: 3:稳定性:相同的关键字计算后,次序是否不变. 简单排序方法 .直接插入排序 直接插入排序 ...
- 快速沃尔什变换FWT
快速沃尔什变换\(FWT\) 是一种可以快速完成集合卷积的算法. 什么是集合卷积啊? 集合卷积就是在集合运算下的卷积.比如一般而言我们算的卷积都是\(C_i=\sum_{j+k=i}A_j*B_k\) ...
- 关于matlab浮点转定点总结
1,算式长度不应该太长,否则在转换过程中提示位宽超过128位,(用的64位matlab),长算式改为短算式就可以了. 2,不要过于相信推荐字长,有些地方需要更高的精度,如果用推荐字长,可能结果误差较大 ...
- 【2】基于zookeeper,quartz,rocketMQ实现集群化定时系统
<一>项目结构图 (1)ZK协调分配 ===>集群中的每一个定时服务器与zookeeper交互,由集群中的master节点进行任务划分,并将划分结果分配给集群中的各个服务器节点. = ...
- LG5055 【模板】可持久化文艺平衡树
题意 您需要写一种数据结构,来维护一个序列,其中需要提供以下操作(对于各个以往的历史版本): 在第 pp 个数后插入数 xx . 删除第 pp 个数. 翻转区间 [l,r][l,r],例如原序列是 { ...
- 洛谷 P2626 斐波那契数列(升级版)
题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数). 题目描述 ...
- 数据库中字段的数据类型与JAVA中数据类型的对应关系
类型名称 显示长度 数据库类型 JAVA类型 JDBC类型索引(int) 描述 VARCHAR L+N VARCHAR java.lang.String 12 CHAR N ...
- c#代碼小集
一.字符串[Uri]轉換出RouteData private RouteData UriToRouteData(Uri uri) { var query = uri.Query; ) { query ...
- Swift-自定制带有特殊按钮TabBar
---恢复内容开始--- 封装了一个带有中间凸起的自定制Tabbar,包含4个普通按钮和中间的一个凸起按钮- 首先封装了一个UIButton,重新设置了UIButton的图片位置和label位置 使用 ...