AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79072496
Accepted
user8253548
user8253548
Asked: 2024-10-10 09:40:15 +0800 CST2024-10-10 09:40:15 +0800 CST 2024-10-10 09:40:15 +0800 CST

使用 Zig comptime 生成结构字段名称时出错

  • 772

我正在尝试编写一个使用 comptime 生成结构字段名称的 Zig 程序,但是在构建项目时遇到错误。

Zig 版本:0.13.0

下面是我的代码:

const std = @import("std");

var person_field_names = generateFieldNames(Person);

fn generateFieldNames(comptime T: type) []const []const u8 {
    comptime {
        const typeInfo = @typeInfo(T);
        switch (typeInfo) {
            .Struct => |structInfo| {
                const field_count = structInfo.fields.len;
                var field_names: [field_count][]const u8 = undefined;

                var i: usize = 0;
                while (i < field_count) : (i += 1) {
                    const field = structInfo.fields[i];
                    field_names[i] = field.name;
                }
                return &field_names;
            },
            else => @compileError("Only structs are supported!"),
        }
    }
}

const Person = struct {
    name: []const u8,
    age: u8,
};

pub fn main() void {
    for (person_field_names) |name| {
        std.debug.print("Field name: {s}\n", .{name});
    }
}

当我运行 zig build run 时,出现以下错误:

$ zig build run
run
└─ run comptime_test
   └─ zig build-exe comptime_test Debug native failure
error: the following command exited with error code 3:
C:\softs\zig\zig.exe build-exe -ODebug -Mroot=C:\workspace\zig\comptime_test\src\main.zig --cache-dir C:\workspace\zig\comptime_test\zig-cache --global-cache-dir C:\Users\myhfs\AppData\Local\zig --name comptime_test --listen=-
Build Summary: 0/5 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run comptime_test transitive failure
   ├─ zig build-exe comptime_test Debug native failure
   └─ install transitive failure
      └─ install comptime_test transitive failure
         └─ zig build-exe comptime_test Debug native (reused)
error: the following build command failed with exit code 1:
C:\workspace\zig\comptime_test\zig-cache\o\9c79a56d0821539e91985b5f4384125d\build.exe C:\softs\zig\zig.exe C:\workspace\zig\comptime_test C:\workspace\zig\comptime_test\zig-cache C:\Users\myhfs\AppData\Local\zig --seed 0xcf452477 -Z3cc02acbdb7abbe2 run

是什么原因导致了此错误?我该如何修复它?

错误代码似乎与我处理字段名称数组的方式有关。我相信问题出在 comptime 处理或从函数返回字段名称上。

zig
  • 1 1 个回答
  • 19 Views

1 个回答

  • Voted
  1. Best Answer
    sigod
    2024-10-10T10:24:48+08:002024-10-10T10:24:48+08:00

    标准库有一个函数可以执行您要执行的操作:std.meta.fieldNames。

    我已设法通过替换让您的代码工作

    return &field_names;
    

    和

    const final = field_names;
    return &final;
    

    我在 中看到的一个技巧std.meta.fieldNames。以下是其当前源代码(Zig 0.13.0),供参考:

    pub fn fieldNames(comptime T: type) *const [fields(T).len][:0]const u8 {
        return comptime blk: {
            const fieldInfos = fields(T);
            var names: [fieldInfos.len][:0]const u8 = undefined;
            // This concat can be removed with the next zig1 update.
            for (&names, fieldInfos) |*name, field| name.* = field.name ++ "";
            const final = names;
            break :blk &final;
        };
    }
    
    • 0

相关问题

  • 有没有办法在 zig 中编写存根函数?

  • randomString 函数中的内存泄漏

  • 如何处理 Zig 测试中使用的辅助函数?

  • 检查 Zig 中是否存在文件

  • 如何转义不可打印字符

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve