10、属性和数据处理---c++17

一、[[fallthrought]]

用途:在 switch 语句中标记某个分支 (case) 故意不写 break,明确告知编译器“执行穿透”是有意为之。

  • 仅在需要向下穿透时使用,且应添加注释说明原因
#include<cstdio>
#include<iostream>
using namespace std;
void do_to(){
    printf("do_to");
}
void do_(){
    printf("do_");
}
int main(int argc,const char* argv[])
{
    int value;
    scanf("%d",&value);
    switch(value){
    case 1:
        do_();
        [[fallthrought]];//明确告知编译器允许穿透
    case 2:
        do_to();
        break;
    default:
        break;
    }
    return 0;
}

二、[[nodiscard]]

用途:强制要求必须处理函数返回值或对象构造结果,防止资源泄漏或逻辑错误。

(表示函数的返回值没有被接收,在编译时会出现警告。)

  • 函数类型
#include<cstdio>
using namespace std;
[[nodiscard]] int createResource() {
    return 42; // 返回资源句柄,调用者必须处理
}

int main(int argc, char const *argv[])
{
    // 调用时会警告
    //错误写法createResource(); // 警告:忽略 nodiscard 返回值
    //正确写法
    int n =createResource();
    return 0;
}


  • 结构体类型
#include<cstdio>
using namespace std;
struct [[nodiscard]] ErrorCode {
    int code;
};
int main(int argc, char const *argv[])
{
    //错误写法ErrorCode parseInput(); // 必须检查返回值
    ErrorCode parseInput{1};//正确写法
    return 0;
}
  • class类型
#include<cstdio>
using namespace std;
class [[nodiscard]] DatabaseConnection { 
public:
    DatabaseConnection(int a):a(a){

    }
public:
    int a;
};
int main(int argc, char const *argv[])
{

    //错误写法DatabaseConnection connect(); // 必须获取连接对象,不能直接丢弃
    //正确写法
    DatabaseConnection connect(1);
    return 0;
}




三、[[maybe_unused]]

用途:显式标记未使用的变量、函数或参数是预期行为,避免编译器警告。

  • 未使用的参数
void logMessage([[maybe_unused]] const std::string& msg) {
    // 代码中暂时未使用 msg,但保留接口兼容性
}

  • 未使用的变量
void process() {
    [[maybe_unused]] int debugCounter = 0; // 调试用变量,发布版可能未使用
}

四、variant

用途:是一个加强版的union,类型安全的联合体,允许保存多种可能的类型值,比如string,map等等。

表示一个可能存在的值。 当我们通过函数创建一个对象时,通常使用通过函数返回错误码,而通过出参返回对象本身。

需要头文件<variant>

#include<csdio>
#include<variant>
using namespace std;
int main() { // c++17可编译
    std::variant<int, std::string> var("hello");
    cout << var.index() << endl;//1
    var = 123;
    cout << var.index() << endl;//0

    try {
        var = "world";
        std::string str = std::get<std::string>(var); // 通过类型获取值
        var = 3;
        int i = std::get<0>(var); // 通过index获取对应值
        cout << str << endl;//world
        cout << i << endl;//3
    } catch(...) {
        // xxx;
    }
    return 0;
}




五、optional

用途:表示一个可能存在或不存在的值,替代 nullptr 或特殊值判断。

需要头文件<optional>

  • 构造和赋值
std::optional<int> opt1;          // 空optional
std::optional<int> opt2 = 42;     // 含值42
std::optional<int> opt3 = opt2;   // 拷贝构造
opt1 = 10;                        // 赋值为10
opt1 = std::nullopt;              // 重置为空
  • 访问值
std::optional<std::string> opt = "Hello";

// 安全访问方式
if (opt) {                     // 检查是否有值
    std::cout << *opt;         // 解引用访问
    std::cout << opt.value();  // 成员函数访问
}

// 不安全访问(可能抛出异常)
try {
    std::cout << opt.value();  // 无值时抛出std::bad_optional_access
} catch(...) {}

// 提供默认值
std::cout << opt.value_or("default");  // 无值时返回"default"
  • has_value():检查是否有值。

  • value():获取值(无值时抛异常)。

  • value_or(default):有值返回,否则返回默认值。

六、any

用途:存储任意类型的单个值,类似类型安全的 void*。

需要头文件<any>

#include <any>
#include <string>

std::any data;
data = 42;                     // 存储 int
data = std::string("hello");   // 存储 string

// 检查并获取值
if (data.type() == typeid(int)) {
    int val = std::any_cast<int>(data);
} else if (data.type() == typeid(std::string)) {
    std::string s = std::any_cast<std::string>(data);
}

七、string_view

  • std::string_view 是 C++17 引入的轻量级字符串视图类,用于高效地处理字符串数据而不涉及内存分配或所有权。`

    • string_view` 作为只读字符串参数
  • 需要头文件<string_view>

八、简化重复命令空间的属性列表

[[ using CC: opt(1), debug ]] void f() {}
//作用相同于 [[ CC::opt(1), CC::debug ]] void f() {}
  • 例如

在C++17之前,属性(attributes)的使用需要显式指定命名空间。

[[rpr::kernel]]
[[rpr::target(cpu, gpu)]]
void compute() {
    // Perform computations
}

C++17后允许在同一个命名空间下声明多个属性时省略重复的命名空间部分。

[[rpr::kernel, rpr::target(cpu, gpu)]]
void compute() {
    // Perform computations
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值