gpt4 book ai didi

c++ - 是否可以替换/或 .在宏中带有 __ 的字符串中?

转载 作者:行者123 更新时间:2023-11-30 16:08:17 25 4
gpt4 key购买 nike

我想用/或 转换字符串。到自动生成的变量名中。所以我需要转换/或 。转换为变量命名可接受的内容,例如 __。

使用函数很容易做到这一点。但是用宏,这可能吗?

#define ROUTE(path, impl) \
char * k##path##_route = "{"#path":\""#impl"\"}"; // compilation failure for path like /usr/abc or ./abc

我可以使用 impl 来命名变量,但由于多个路径可能映射到同一个 impl,编译器会提示具有相同 impl 的不同路径的重复。

最佳答案

But with macro, is it possible?

没有。无法使用标准预处理器替换参数的字符。

acceptable by variable naming, such as __

__ 不是可接受的变量名称,因为它是为实现保留的。 C++ 中所有包含连续下划线的标识符也是如此。

关于c++ - 是否可以替换/或 .在宏中带有 __ 的字符串中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59393006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com