在 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++ 代码。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐