首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法在编译阶段用子字符串替换char*字符串?

有没有办法在编译阶段用子字符串替换char*字符串?
EN

Stack Overflow用户
提问于 2022-06-15 07:08:15
回答 1查看 154关注 0票数 -3

我想要做的是用constexpr函数替换字符串,例如:

代码语言:javascript
复制
constexpr auto replace_sub_str(const char* x) {
  // some magic
  
  return ans;
}

#define LOG(x) replace_sub_str(x)

示例:LOG("hello XX, XX");,它将被替换为"hello KK, KK"

我更新了这个问题,因为也许奇怪的人物让人困惑。这里XX只是一个子字符串,KK也是。

EN

回答 1

Stack Overflow用户

发布于 2022-06-15 07:14:16

是:

代码语言:javascript
复制
template <auto N>
constexpr auto replace_sub_str(char const (&src)[N]) {
   std::array<char, N> res = {}; 
   // do whatever string manipulation you want in res.
   return res;
}


auto constexpr str = replace_sub_str("hello {}, {}");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72627188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档