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

#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.

C++ essentials 之 explicit constructor的更多相关文章

  1. 错误:Implicit super constructor xx() is undefined for default constructor. Must define an explicit constructor

    错误:Implicit super constructor xx() is undefined for default constructor. Must define an explicit con ...

  2. Eclipse错误:Implicit super constructor ClassName is undefined for default constructor. Must define an explicit constructor

    public class Test01 { private String name; private int age; public Test01(String name){ this.name = ...

  3. C++ explicit constructor/copy constructor note

    C++:explict 作用显示声明构造函数只能被显示调用从而阻止编译器的隐式转换,类似只能用()显示调用,而不能=或者隐式调用 #include <iostream> #include ...

  4. Implicit super constructor xx() is undefined for default constructor. Must define an explicit constructor

      错误:Implicit super constructor xx() is undefined for default constructor. Must define an explicit c ...

  5. explicit constructor(显示构造函数)

    按照默认规定,只有一个参数的构造函数也定义了一个隐式转换,将该构造函数对应的数据类型的数据转换为该类对象,如下所示: class String { String(const char* p) //用C ...

  6. 子类继承父类时JVM报出Error:Implicit super constructor People() is undefined for default constructor. Must define an explicit constructor

    当子类继承父类的时候,若父类没有定义带参的构造方法,则子类可以继承父类的默认构造方法 当父类中定义了带参的构造方法,子类必须显式的调用父类的构造方法 若此时,子类还想调用父类的默认构造方法,必须在父类 ...

  7. Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor

    转自:https://blog.csdn.net/u013125680/article/details/43887987 解决方案:把java的类库加载进去,在工程上右键选择属性->Java B ...

  8. Implicit super constructor Array() is undefined for default constructor. Must define an explicit constructor

    因为你的父类已经创建了一个带参的构造函数并且父类中没有无参的构造函数,此时编译器不会为你调用默认的构造函数, 所以子类在继承父类的时候需要在自己的构造函数中显式的调用父类的构造函数,这样才能确保子类在 ...

  9. 第8课 列表初始化(3)_防止类型收窄、explicit关键字

    1. 防止类型收窄 (1)类型收窄:指的是导致数据内容发生变化或者精度丢失的隐式类型转换. (2)类型收窄的几种情况: ①从浮点数隐式转换为整型数,如int i=2.2; ②从高精度浮点数隐式转换为低 ...

随机推荐

  1. 【洛谷2577】[ZJOI2005] 午餐(较水DP)

    点此看题面 大致题意: 有\(N\)个学生去食堂打饭,每个学生有两个属性:打饭时间\(a_i\)和吃饭时间\(b_i\).现要求将这些学生分成两队分别打饭,求最早何时所有人吃完饭. 贪心 首先,依据贪 ...

  2. 【洛谷2216】[HAOI2007] 理想的正方形(二维RMQ)

    点此看题面 大致题意: 求出一个矩阵中所有\(n*n\)正方形中极差的最小值. 另一种做法 听说这题可以用单调队列去做,但是我写了一个二维\(RMQ\). 二维\(RMQ\) \(RMQ\)相信大家都 ...

  3. python基础一 day17 作业

    # 3.用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sbname=['alex','wupeiqi','yuanhao','nezha']# def func(item):# r ...

  4. vue2.0在页面中自定义组件模块,以及页面与组件之间的数据传递

    1,在初始文件index.html中加入要引入的模块,注意驼峰命名的方式(我就是没写成驼峰,报错) <!DOCTYPE html> <html> <head> &l ...

  5. 去除myeclipse中doget和dopost方法中的注释

    当我们使用myeclipse新建servlet时发现doget和dopost方法中有一些无用的注释,每次新建一个servlet时都要手动删除特别麻烦. 下面就教大家如何去除这些注释! 以myeclip ...

  6. (SSO)单点登录原理和总结

    一:什么是单点登录(single Sign-On) SSO 是一种统一认证和授权机制,指访问统一服务器不用应用中的受保护资源的同一用户,只需要登录一次,即通过一个应用中的安全验证后, 再访问其他应用中 ...

  7. Ecshop里添加多个h1标题

    目录 功能: 思路: 效果: pageheader_list.htm里 product_sn_list.htm模板里 控制器里 功能: 点击页面右边的两个按钮,切换下面的<div class=& ...

  8. django-simple-captcha 验证码干扰线随机点位

    CAPTCHA_NOISE_FUNCTIONS = ( 'captcha.helpers.noise_null',# 设置样式 'captcha.helpers.noise_arcs',# 设置干扰线 ...

  9. [译]The Python Tutorial#7. Input and Output

    [译]The Python Tutorial#Input and Output Python中有多种展示程序输出的方式:数据可以以人类可读的方式打印出来,也可以输出到文件中以后使用.本章节将会详细讨论 ...

  10. kubernetes dashboard permission errors

    kubernetes dashboard 的权限错误 warning configmaps is forbidden: User "system:serviceaccount:kube-sy ...