这篇博客的源起是下面的一段代码

#include <bits/stdc++.h>
using namespace std; int main(){
priority_queue<long long> que;
// some operations on que
que = {};
// some operations on que
return 0;
}

其中 que = {} 相当于 que.clear();std::priority_queue 并没有 clear() 方法)。以前我清空 priority_queue 用的是

while(!que.empty()){
que.pop();
}

然而编译器对这个语句给出了一个警告:

(g++ 6.3.0:g++ -Wall -std=c++14

In function 'int main()':
7:12: warning: converting to 'std::priority_queue<long long int>' from initializer list would use explicit constructor 'std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, _Sequence&&) [with _Tp = long long int; _Sequence = std::vector<long long int, std::allocator<long long int> >; _Compare = std::less<long long int>]'
que = {};
^
7:12: note: in C++11 and above a default constructor can be explicit

概念

implicit class-type conversion

Every constructor that can be called with a single argument defines an implicit conversion to a class type. Such constructors are sometimes referred to as conversion constructors.

explicit constructors

We can prevent the use of a constructor in a context that requires an implicit conversion by declaring the constructor as explicit.

The explicit keyword is meaningful only on constructors that can be called with a single argument. Constructors that require more arguments are not used to perform a conversion, so there is no need to designate such constructors as explicit. The explicit keyword is used only on the constructor declaration inside the class. It is not repeated on a definition made outside the class body.

One context in which implicit conversions happen is when we use the copy form of initialization (with an =). An explicit constructor cannot be used with the copy form of initialization; it can be used only with the direct form of initialization. Moreover, the compiler will not use this constructor in an automatic conversion.

最新文章

  1. PPT转化成Image、PPTX、XPS、EMF
  2. ENode通信层性能测试结果
  3. Module Zero之用户管理
  4. 设置SQLServer的行版本控制隔离级别
  5. C#基础知识系列六(静态类和静态类成员)
  6. 在cmd下编译一个简单的servlet时出现程序包javax.servlet不存在
  7. spark 分析sql内容再插入到sql表中
  8. SSDB
  9. MyEclipse使用手册(详细版)
  10. 几种解析xml方式的比较
  11. 使用autoCompleteTextView以及MultiAutoCompleteTextView实现自动匹配输入内容
  12. 2016WHD.china世界云计算日&#183;北京站即将召开
  13. Ubuntu 虚拟机支持与windows机器复制粘贴
  14. C# 函数式编程 —— 使用 Lambda 表达式编写递归函数
  15. TensorFlow激活函数+归一化-函数
  16. 使用 Nginx 为 Linux 实例绑定多个域名
  17. java中一对多 关联建表
  18. 广播 (Broadcast)
  19. 【vue】Mac上安装Node和NPM
  20. 【PaPaPa】系统架构搭建浅析 - 人人可以搭架构

热门文章

  1. Linux 的歷史
  2. 在O(1)时间复杂度删除链表节点
  3. 黑马基础阶段测试题:通过字符输入流读取info.txt中的所有内容,每次读取一行,将每一行的第一个文字截取出来并打印在控制台上。
  4. 关于小程序 input 组件内容显示不全(显示的长度不满 input 宽度)问题
  5. 《JavaScript入门篇》摘要
  6. C++ 学习笔记 (八)重载 重写 重定义以及名字覆盖
  7. 一次磁盘IO过高分析过程
  8. Docker容器学习--1
  9. 1px移动端显示问题
  10. Redis之set类型操作