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

#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. antd-design-pro 服务代理问题

    公司希望又一个后台管理页面.因为之前技术栈是react 所以选择了antd-design-pro作为后台的框架. 在连调api的时候,困惑怎么去代理.因为网上查到很多都是1.0的版本,而我现在用的是2 ...

  2. 螺旋矩阵,两步进阶,从暴力到o(1)

    题目描述 一个 n 行 n 列的螺旋矩阵可由如下方法生成: 从矩阵的左上角(第 1 行第 1 列)出发,初始时向右移动:如果前方是未曾经过的格子,则继续前进,否则右转:重复上述操作直至经过矩阵中所有格 ...

  3. 线段树和zkw线段树

    作者作为一个蒟蒻,也是最近才自学了线段树,不对的地方欢迎大佬们评论,但是不要喷谢谢 好啦,我们就开始说说线段树吧 线段树是个支持区间操作和查询的东东,平时的话还是蛮实用的 下面以最基本的区间加以及查询 ...

  4. 什么是 TCP/IP?

    前言: 精通 TCP/IP,熟练使用 Socket 进行网路编程. 看到这句话,有没有感到很熟悉呀?相信很多人在投递简历的时候都看到过这条要求,很多人会觉得我们在实际开发中一般用不到这些知识,所以对这 ...

  5. Mysql的一些纪要

    unsigned 整型的每一种都分无符号(unsigned)和有符号(signed)两种类型(float和double总是带符号的),在默认情况下声明的整型变量都是有符号的类型(char有点特别),如 ...

  6. Centos7之Nginx

    1.安装 下载RPM: wget http://nginx.org/download/nginx-1.16.0.tar.gz 解压:tar -zxf nginx-1.16.0.tar.gz 安装: c ...

  7. Linux入门-第七周

    1.编写脚本实现传入进程PID,查看对应进程/proc下CPU.内存指标. #!/bin/bash read -p "Input PID Value: " pid #读取PID进程 ...

  8. nginx下配置Yii2 rewrite、pathinfo等

    环境说明: 我试用的lnmp安装包安装的nginx,nginx版本是1.14.1 server { listen ; server_name www.baidu.com; #access_log /d ...

  9. php面向对象(2)构造和析构函数

    一.构造方法 构造方法是类中一个“特殊”的方法,作用是在实例化一个对象的同时,给该对象的属性赋值,使之创建完成的时就具有其本身的特有属性 该方法固定格式:[访问修饰符] function _const ...

  10. JZOJ 4722. 跳楼机

    Description  DJL为了避免成为一只咸鱼,来找srwudi学习压代码的技巧.Srwudi的家是一幢h层的摩天大楼.由于前来学习的蒟蒻越来越多,srwudi改造了一个跳楼机,使得访客可以更方 ...