std::format 如何实现编译期格式检查

打印 上一主题 下一主题

主题 860|帖子 860|积分 2580

C++ 20 的 std::format 是一个很神奇、很实用的工具,最神奇的地方在于它能在编译期检查字符串的格式是否正确,而且不需要什么特殊的利用方法,只需要像利用平凡函数那样传参即可。
  1. #include <format>
  2. int a = 1;
  3. std::string s1 = std::format("a: {}", a); // OK
  4. std::string s2 = std::format("a: {}, b: {}", a); // 编译错误
复制代码
C++ 20 的 std::format 来自一个著名的开源库 {fmt}。在 C++ 20 之前,fmt 需要为每个字符串字面量创建不同的类型才气实现编译期格式检查。fmt 提供了一个 FMT_STRING 宏以简化利用的流程。
  1. #include <fmt/format.h>
  2. int a = 1;
  3. std::string s1 = fmt::format(FMT_STRING("a: {}"), a); // OK
  4. std::string s2 = fmt::format(FMT_STRING("a: {}, b: {}"), a); // 编译错误
复制代码
C++ 20 有了 consteval 后就不用这么别扭了。consteval 函数与从前的 constexpr 函数不同,constexpr 函数只有在必须编译期求值的语境下才会在编译期执行函数,而 consteval 函数在任何环境下都强制编译期求值。std::format 就是利用 consteval 函数在编译期执行代码,来检查字符串参数的格式。
然而 std::format 自身不能是 consteval 函数,只好曲线救国,引入一个辅助类型 std::format_string,让字符串实参隐式转换为 std::format_string。只要这个转换函数是 consteval 函数,并且把格式检查的逻辑写在这个转换函数里面,照样能实现编译期的格式检查。
这里我们实现了一个极简版的 format,可以检查字符串中 {} 的数量是否与参数的个数相同。format_string 的构造函数就是我们需要的隐式转换函数,它是一个 consteval 函数。若字符串中 {} 的数量不对,则代码会执行到 throw 这一行。C++ 的 throw 语句不能在编译期求值,因此会引发编译错误,从而实现了在编译期检查出字符串的格式错误。
  1. namespace my {
  2.     template<class ...Args>
  3.     class format_string {
  4.     private:
  5.         std::string_view str;
  6.     public:
  7.         template<class T>
  8.             requires std::convertible_to<const T &, std::string_view>
  9.         consteval format_string(const T &s)
  10.             : str(s)
  11.         {
  12.             std::size_t actual_num = 0;
  13.             for (std::size_t i = 0; i + 1 < str.length(); i++) {
  14.                 if (str[i] == '{' && str[i + 1] == '}') {
  15.                     actual_num++;
  16.                 }
  17.             }
  18.             constexpr std::size_t expected_num = sizeof...(Args);
  19.             if (actual_num != expected_num) {
  20.                 throw std::format_error("incorrect format string");
  21.             }
  22.         }
  23.         std::string_view get() const { return str; }
  24.     };
  25.     template<class ...Args>
  26.     std::string format(format_string<std::type_identity_t<Args>...> fmt, Args &&...args) {
  27.         // 省略具体的格式化逻辑
  28.     }
  29. }
复制代码
有一个细节,此处 format 函数的参数写的是 format_string,直接写 format_string 是无法隐式转换的,因为模板实参推导 (template argument deduction) 不会考虑隐式转换,C++ 20 提供了一个工具 std::type_identity 可以办理这个问题。std::type_identity 其实就是一个关于类型的恒等函数,但是这么倒腾一下就能在模板实参推导中建立非推导语境 (non-deduced context),进而正常地匹配到隐式转换,C++ 就是这么奇怪。参考资料:c++ - why would type_identity make a difference? - Stack Overflow

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

渣渣兔

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表