"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 ...
随机推荐
- [转帖]TiDB-merge region相关问题
一.开启region merge # 控制 Region Merge 的 size 上限,当 Region Size 大于指定值时 PD 不会将其与相邻的 Region 合并 pd-ctl confi ...
- [转帖]CentOS7使用Chrony实现时间同步
学习安装部署 ceph 时 ,在添加 mon 时报错了,搜索原因后发现是 时间同步问题.于是学习一下时间同步工具. 一般CentOS6 使用的时间同步工具是ntp.现在还有不少开源软件文档建议安装的时 ...
- [转帖]kingbase(人大金仓)的一些常用表操作语句
包括 1)创建表 2)删除表 3)加字段 4)字段换名 5)字段改类型 6)字段添加注释 7)修改字段为自增类型 8)增加主键 9)查看模式下的表 一.创建和删除表 DROP TABLE IF EXI ...
- [转帖]Linux系统下rpm命令使用详解
简介 rpm命令是RPM软件包的管理工具.rpm原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序,由于它遵循GPL规则且功能强大方便,因而广受欢迎.逐渐受到其他发行版的采用. ...
- rfc7230 Message Syntax and Routing
rfc7230 目录 rfc7230 2 Architecture 2.6 Protocol Versioning 3 Message Format 3.1 Start Line 3.1.1 Requ ...
- 【DP】DMOPC '21 Contest 8 P5 - Tree Building
Problem Link 给定 \(n,m\) 和一个长为 \(m\) 的代价序列,对于一棵 \(n\) 个节点,每个节点度数不超过 \(m\) 的树,定义它的代价为 \(\sum\limits_{i ...
- 学习React中的jsx-保证你看的明明白白
安装react的脚手架 1==>行 npx create-react-app myreactdemo01 解释一下: npx create-react-app 你的项目名称 npx create ...
- Protobuf中如何指定json tag
在 Protocol Buffers (protobuf) 中,可以使用特定的选项来指定生成的 JSON 标签.通过在消息定义中使用 [(json_name)] 选项,可以控制生成的 JSON 字段名 ...
- [2] HEVD 学习笔记:栈溢出漏洞训练
2. HEVD 栈溢出漏洞训练 2.1 漏洞原理 当函数退出的时候,会将保存在栈中的返回地址取出,跳转到该地址继续执行,以此来执行函数调用以后的程序.而如果用户的输入没有得到控制,覆盖掉了这个返回 ...
- TienChin 渠道管理-权限分配
添加权限 如果您不想手动添加可以使用我如下的SQL,但是有一个注意点就是 parent_id 是渠道管理菜单的主键 id 即可一键插入. INSERT INTO `TienChin`.`sys_men ...