c++17中的属性[[nodiscard]]的作用及说明
作者:赵民勇
这篇文章主要介绍了c++17中的属性[[nodiscard]]的作用及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
在 C++17 中,[[nodiscard]] 是一个属性(attribute),用于向编译器表明某个函数的返回值不应该被忽略(丢弃)。
主要作用
1. 强制检查返回值
当函数被声明为 [[nodiscard]] 时,如果调用者没有使用返回值,编译器会产生警告。
[[nodiscard]] int calculateImportantValue() {
return 42;
}
void example() {
calculateImportantValue(); // 警告:忽略了 nodiscard 函数的返回值
int result = calculateImportantValue(); // 正确:使用了返回值
}2. 应用于类/结构体
可以将 [[nodiscard]] 应用于整个类,这样该类的所有构造函数返回值都不应被忽略。
class [[nodiscard]] ImportantResource {
public:
ImportantResource() { /* 获取资源 */ }
~ImportantResource() { /* 释放资源 */ }
};
void example() {
ImportantResource(); // 警告:创建了临时对象但立即销毁
ImportantResource res; // 正确:有名字的对象
}3. 应用于枚举
也可以将 [[nodiscard]] 应用于枚举类型。
enum class [[nodiscard]] ErrorCode {
Success,
Failure
};
ErrorCode performOperation() {
return ErrorCode::Success;
}
void example() {
performOperation(); // 警告:忽略了返回值
}实际应用场景
资源管理
class FileHandle {
public:
[[nodiscard]] static FileHandle open(const char* filename) {
// 打开文件
return FileHandle(/* ... */);
}
void close() { /* 关闭文件 */ }
};
void example() {
FileHandle::open("file.txt"); // 警告:文件句柄被立即丢弃!
// 正确用法
auto handle = FileHandle::open("file.txt");
// 使用文件...
handle.close();
}错误处理
[[nodiscard]] bool initializeSystem() {
// 系统初始化
return true; // true 表示成功
}
void example() {
initializeSystem(); // 警告:没有检查初始化是否成功
if (!initializeSystem()) { // 正确:检查返回值
// 处理错误
}
}内存分配
class MemoryManager {
public:
[[nodiscard]] void* allocate(size_t size) {
return malloc(size);
}
};
void example() {
MemoryManager manager;
manager.allocate(1024); // 警告:内存泄漏风险!
void* ptr = manager.allocate(1024); // 正确
// 使用内存...
free(ptr);
}编译器支持
- GCC 7+
- Clang 3.9+
- MSVC 2017 15.3+
总结
[[nodiscard]] 是一个很有用的属性,它可以帮助:
- 防止资源泄漏
- 强制错误检查
- 提高代码安全性
- 在编译时捕获潜在的错误
通过合理使用这个属性,可以编写出更安全、更健壮的 C++ 代码。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
