"explicit" 的使用
今天在编译项目时,代码审查提示 “Single-parameter constructors should be marked explicit”
于是就在构造函数前加上 explicit
下面为最小的示例,这个示例主要是按文件创建时间排序并删除过期的文件
#include <iostream>
#include <thread>
#include <windows.h>
#include <exception>
#include <vector>
#include <filesystem> wchar_t file_path[] =
L"D:/VS2019_Program/Test_SetUnhandledExceptionFilter/Debug/.sentry-native/reports/"; wchar_t file_ext[] = L".dmp"; std::vector<std::wstring> TraverseFolder(std::wstring folder_path,
std::wstring ext); class file {
public:
std::wstring name;
FILETIME time; public:
bool operator<(file const& other) const {
return CompareFileTime(&other.time, &time) == 1;
} file(WIN32_FIND_DATA const &d) : name(d.cFileName), time(d.ftCreationTime) {}
}; std::vector<file> SortDumpFiles(std::vector<std::wstring> dumpfiles) {
std::vector<file> files; std::transform(dumpfiles.begin(), dumpfiles.end(), std::back_inserter(files),
[](std::wstring const &fname) {
WIN32_FIND_DATA d;
HANDLE h = FindFirstFile(fname.c_str(), &d);
FindClose(h);
return d;
}); std::sort(files.begin(), files.end()); return files;
} void DelExpiredDumpFiles() {
std::wstring path(file_path);
std::wstring ext(file_ext);
auto dump_file = TraverseFolder(path, ext); int total_file = dump_file.size();
int to_be_deleted = total_file - 4;
if (to_be_deleted <= 0) {
return;
}
// 以文件创建时间
auto files = SortDumpFiles(dump_file);
while (to_be_deleted--) {
std::wstring file = file_path;
file += files[to_be_deleted].name;
if (!DeleteFile(file.c_str())) {
int err = GetLastError();
return;
}
}
} std::vector<std::wstring> TraverseFolder(std::wstring folder_path,
std::wstring ext) {
std::vector<std::wstring> filename;
if (!std::filesystem::is_directory(folder_path)) {
return filename;
} for (auto& p : std::filesystem::recursive_directory_iterator(folder_path)) {
if (p.path().extension() == ext) {
filename.push_back(p.path().wstring());
}
}
return filename;
} int main() {
DelExpiredDumpFiles(); return 0;
}
添加 explicit 关键词
explicit file(WIN32_FIND_DATA const &d) : name(d.cFileName), time(d.ftCreationTime) {}
编译会报错,

解决方法,
std::transform(dumpfiles.begin(), dumpfiles.end(), std::back_inserter(files),
[](std::wstring const &fname) {
WIN32_FIND_DATA d;
HANDLE h = FindFirstFile(fname.c_str(), &d);
FindClose(h);
return file{d};
});
原因:
explicit 关键字
- 指定构造函数或转换函数 (C++11起)为显式, 即它不能用于隐式转换和复制初始化.
- explicit 指定符可以与常量表达式一同使用. 函数若且唯若该常量表达式求值为 true 才为显式. (C++20起)
也就是构造函数被 explicit 修饰后, 就不能再被隐式调用了
等于说,std::vector<file> files = d 这样的操作禁止使用了,只能 std::vector<file> files = files{d}
Effective C++中也阐述了:
被声明为
explicit的构造函数通常比其 non-explicit 兄弟更受欢迎, 因为它们禁止编译器执行非预期 (往往也不被期望) 的类型转换. 除非我有一个好理由允许构造函数被用于隐式类型转换, 否则我会把它声明为explicit. 我鼓励你遵循相同的政策.
"explicit" 的使用的更多相关文章
- 可空类型(Nullable<T>)及其引出的关于explicit、implicit的使用
问题一:Nullable<T>可赋值为null 先看两行C#代码 int? i1 = null; int? i2 = new int?(); int? 即Nullable<int&g ...
- 关于Django 错误 doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS
记录一下 报错 doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS\ 这个问题出现没 ...
- 显示转换explicit和隐式转换implicit
用户自定义的显示转换和隐式转换 显式转换implicit关键字告诉编译器,在源代码中不必做显示的转型就可以产生调用转换操作符方法的代码. 隐式转换implicit关键字告诉编译器只有当源代码中指定了显 ...
- explicit抑制隐型转换
本文出自 http://www.cnblogs.com/cutepig/ 按照默认规定,只有一个参数的构造函数也定义了一个隐式转换,将该构造函数对应数据类型的数据转换为该类对象,如下面所示: clas ...
- C++ explicit关键字详解
本文系转载,原文链接:http://www.cnblogs.com/ymy124/p/3632634.html 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用 ...
- Implicit and Explicit Multithreading MULTITHREADING AND CHIP MULTIPROCESSORS
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The concept of thread ...
- 【Android 疑难杂症1】android.content.ActivityNotFoundException: Unable to find explicit activity class
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.cnote ...
- C++笔记(1)explicit构造函数
按照默认规定,只有一个参数的构造函数也定义了一个隐式转换,将该构造函数对应数据类型的数据转换为该类对象,如下面所示: class String { String ( const char* p ); ...
- C++中explicit关键字的使用
看书看到了explicit关键字,就来做个笔记,讲得比较明白,比较浅. 在C++中,我们有时可以将构造函数用作自动类型转换函数.但这种自动特性并非总是合乎要求的,有时会导致意外的类型转换,因此,C++ ...
- 错误: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 ...
随机推荐
- [转帖]ASH、AWR、ADDM区别联系
==================================================================================================== ...
- [转帖]可直接拿来用的kafka+prometheus+grafana监控告警配置
kafka配置jmx_exporter 点击:https://github.com/prometheus/jmx_exporter,选择下面的jar包下载: 将下载好的这个agent jar包上传到k ...
- [转帖]手摸手搭建简单的jmeter+influxdb+grafana性能监控平台
我安装的机器是阿里云的centos8机器,其他的系统暂未验证 1.安装influxdb influxdb 下载地址https://portal.influxdata.com/downloads/,也可 ...
- [转帖]badboy与jmeter的结合使用
https://blog.csdn.net/weixin_41754309/article/details/107106855 欢迎关注[无量测试之道]公众号,回复[领取资源], Python编程学习 ...
- [转帖]【jmeter】BeanShell用法详细汇总
一.什么是Bean Shell BeanShell是用Java写成的,一个小型的.免费的.可以下载的.嵌入式的Java源代码解释器,具有对象脚本语言特性,非常精简的解释器jar文件大小为175k. B ...
- 【转帖】ESXi 6.x 安装storcli监控raid卡状态
https://b2b.baidu.com/land?id=744541c6188f7937d6dc97d6fb9142ff10 脚本宝典收集整理的这篇文章主要介绍了ESXi 6.x 安装storcl ...
- [转帖]华为OpenEuler欧拉系统添加epel源方法
https://blog.whsir.com/post-7002.html 由于国产华为OpenEuler欧拉系统的版本命名是22.03.22.03这种,并且在查看版本的路径中是/etc/open ...
- vue中keep-alive详细讲解
场景 今天产品跑过来跟我说, 当我在A页面修改数据后,去B页面. 然后返回A页面.希望A页面保留我修改后的数据. 而不是数据又恢复最初的样子了.我心里想,尼玛,又要加班了? 看下面小粒子 数据被被重置 ...
- c++基础之函数
距离上次更新又过了一周,又该更新新的读书笔记了.本次更新的主要是c++中函数部分的内容 c++ 中的函数与c语言中的函数大致用法或者语法是一样的,这里就不就这点详细展开了.需要注意的是c/c++中并没 ...
- Docker 安装 Nacos 注册中心
废话不多说直接上安装脚本: 在运行安装脚本之前,首先,我们查看一下 Nacos 的版本分别有哪些使用 docker search nacos: 然后在执行: docker pull nacos/nac ...