compile.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. // Formatting library for C++ - experimental format string compilation
  3. //
  4. // Copyright (c) 2012 - present, Victor Zverovich and fmt contributors
  5. // All rights reserved.
  6. //
  7. // For the license information refer to format.h.
  8. #ifndef FMT_COMPILE_H_
  9. #define FMT_COMPILE_H_
  10. #ifndef FMT_MODULE
  11. # include <iterator> // std::back_inserter
  12. #endif
  13. #include "format.h"
  14. FMT_BEGIN_NAMESPACE
  15. FMT_BEGIN_EXPORT
  16. // A compile-time string which is compiled into fast formatting code.
  17. class compiled_string {};
  18. template <typename S>
  19. struct is_compiled_string : std::is_base_of<compiled_string, S> {};
  20. /**
  21. * Converts a string literal `s` into a format string that will be parsed at
  22. * compile time and converted into efficient formatting code. Requires C++17
  23. * `constexpr if` compiler support.
  24. *
  25. * **Example**:
  26. *
  27. * // Converts 42 into std::string using the most efficient method and no
  28. * // runtime format string processing.
  29. * std::string s = fmt::format(FMT_COMPILE("{}"), 42);
  30. */
  31. #if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
  32. # define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::compiled_string)
  33. #else
  34. # define FMT_COMPILE(s) FMT_STRING(s)
  35. #endif
  36. /**
  37. * Converts a string literal into a format string that will be parsed at
  38. * compile time and converted into efficient formatting code. Requires support
  39. * for class types in constant template parameters (a C++20 feature).
  40. *
  41. * **Example**:
  42. *
  43. * // Converts 42 into std::string using the most efficient method and no
  44. * // runtime format string processing.
  45. * using namespace fmt::literals;
  46. * std::string s = fmt::format("{}"_cf, 42);
  47. */
  48. #if FMT_USE_NONTYPE_TEMPLATE_ARGS
  49. inline namespace literals {
  50. template <detail::fixed_string Str> constexpr auto operator""_cf() {
  51. return FMT_COMPILE(Str.data);
  52. }
  53. } // namespace literals
  54. #endif
  55. FMT_END_EXPORT
  56. namespace detail {
  57. template <typename T, typename... Tail>
  58. constexpr auto first(const T& value, const Tail&...) -> const T& {
  59. return value;
  60. }
  61. #if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
  62. template <typename... T> struct type_list {};
  63. // Returns a reference to the argument at index N from [first, rest...].
  64. template <int N, typename T, typename... Args>
  65. constexpr auto get([[maybe_unused]] const T& first,
  66. [[maybe_unused]] const Args&... rest) -> const auto& {
  67. static_assert(N < 1 + sizeof...(Args), "index is out of bounds");
  68. if constexpr (N == 0)
  69. return first;
  70. else
  71. return detail::get<N - 1>(rest...);
  72. }
  73. # if FMT_USE_NONTYPE_TEMPLATE_ARGS
  74. template <int N, typename T, typename... Args, typename Char>
  75. constexpr auto get_arg_index_by_name(basic_string_view<Char> name) -> int {
  76. if constexpr (is_static_named_arg<T>()) {
  77. if (name == T::name) return N;
  78. }
  79. if constexpr (sizeof...(Args) > 0)
  80. return get_arg_index_by_name<N + 1, Args...>(name);
  81. (void)name; // Workaround an MSVC bug about "unused" parameter.
  82. return -1;
  83. }
  84. # endif
  85. template <typename... Args, typename Char>
  86. FMT_CONSTEXPR auto get_arg_index_by_name(basic_string_view<Char> name) -> int {
  87. # if FMT_USE_NONTYPE_TEMPLATE_ARGS
  88. if constexpr (sizeof...(Args) > 0)
  89. return get_arg_index_by_name<0, Args...>(name);
  90. # endif
  91. (void)name;
  92. return -1;
  93. }
  94. template <typename Char, typename... Args>
  95. constexpr auto get_arg_index_by_name(basic_string_view<Char> name,
  96. type_list<Args...>) -> int {
  97. return get_arg_index_by_name<Args...>(name);
  98. }
  99. template <int N, typename> struct get_type_impl;
  100. template <int N, typename... Args> struct get_type_impl<N, type_list<Args...>> {
  101. using type =
  102. remove_cvref_t<decltype(detail::get<N>(std::declval<Args>()...))>;
  103. };
  104. template <int N, typename T>
  105. using get_type = typename get_type_impl<N, T>::type;
  106. template <typename T> struct is_compiled_format : std::false_type {};
  107. template <typename Char> struct text {
  108. basic_string_view<Char> data;
  109. using char_type = Char;
  110. template <typename OutputIt, typename... T>
  111. constexpr auto format(OutputIt out, const T&...) const -> OutputIt {
  112. return write<Char>(out, data);
  113. }
  114. };
  115. template <typename Char>
  116. struct is_compiled_format<text<Char>> : std::true_type {};
  117. template <typename Char>
  118. constexpr auto make_text(basic_string_view<Char> s, size_t pos, size_t size)
  119. -> text<Char> {
  120. return {{&s[pos], size}};
  121. }
  122. template <typename Char> struct code_unit {
  123. Char value;
  124. using char_type = Char;
  125. template <typename OutputIt, typename... T>
  126. constexpr auto format(OutputIt out, const T&...) const -> OutputIt {
  127. *out++ = value;
  128. return out;
  129. }
  130. };
  131. // This ensures that the argument type is convertible to `const T&`.
  132. template <typename T, int N, typename... Args>
  133. constexpr auto get_arg_checked(const Args&... args) -> const T& {
  134. const auto& arg = detail::get<N>(args...);
  135. if constexpr (detail::is_named_arg<remove_cvref_t<decltype(arg)>>()) {
  136. return arg.value;
  137. } else {
  138. return arg;
  139. }
  140. }
  141. template <typename Char>
  142. struct is_compiled_format<code_unit<Char>> : std::true_type {};
  143. // A replacement field that refers to argument N.
  144. template <typename Char, typename V, int N> struct field {
  145. using char_type = Char;
  146. template <typename OutputIt, typename... T>
  147. constexpr auto format(OutputIt out, const T&... args) const -> OutputIt {
  148. const V& arg = get_arg_checked<V, N>(args...);
  149. if constexpr (std::is_convertible<V, basic_string_view<Char>>::value) {
  150. auto s = basic_string_view<Char>(arg);
  151. return copy<Char>(s.begin(), s.end(), out);
  152. } else {
  153. return write<Char>(out, arg);
  154. }
  155. }
  156. };
  157. template <typename Char, typename T, int N>
  158. struct is_compiled_format<field<Char, T, N>> : std::true_type {};
  159. // A replacement field that refers to argument with name.
  160. template <typename Char> struct runtime_named_field {
  161. using char_type = Char;
  162. basic_string_view<Char> name;
  163. template <typename OutputIt, typename T>
  164. constexpr static auto try_format_argument(
  165. OutputIt& out,
  166. // [[maybe_unused]] due to unused-but-set-parameter warning in GCC 7,8,9
  167. [[maybe_unused]] basic_string_view<Char> arg_name, const T& arg) -> bool {
  168. if constexpr (is_named_arg<typename std::remove_cv<T>::type>::value) {
  169. if (arg_name == arg.name) {
  170. out = write<Char>(out, arg.value);
  171. return true;
  172. }
  173. }
  174. return false;
  175. }
  176. template <typename OutputIt, typename... T>
  177. constexpr auto format(OutputIt out, const T&... args) const -> OutputIt {
  178. bool found = (try_format_argument(out, name, args) || ...);
  179. if (!found) {
  180. FMT_THROW(format_error("argument with specified name is not found"));
  181. }
  182. return out;
  183. }
  184. };
  185. template <typename Char>
  186. struct is_compiled_format<runtime_named_field<Char>> : std::true_type {};
  187. // A replacement field that refers to argument N and has format specifiers.
  188. template <typename Char, typename V, int N> struct spec_field {
  189. using char_type = Char;
  190. formatter<V, Char> fmt;
  191. template <typename OutputIt, typename... T>
  192. constexpr FMT_INLINE auto format(OutputIt out, const T&... args) const
  193. -> OutputIt {
  194. const auto& vargs =
  195. fmt::make_format_args<basic_format_context<OutputIt, Char>>(args...);
  196. basic_format_context<OutputIt, Char> ctx(out, vargs);
  197. return fmt.format(get_arg_checked<V, N>(args...), ctx);
  198. }
  199. };
  200. template <typename Char, typename T, int N>
  201. struct is_compiled_format<spec_field<Char, T, N>> : std::true_type {};
  202. template <typename L, typename R> struct concat {
  203. L lhs;
  204. R rhs;
  205. using char_type = typename L::char_type;
  206. template <typename OutputIt, typename... T>
  207. constexpr auto format(OutputIt out, const T&... args) const -> OutputIt {
  208. out = lhs.format(out, args...);
  209. return rhs.format(out, args...);
  210. }
  211. };
  212. template <typename L, typename R>
  213. struct is_compiled_format<concat<L, R>> : std::true_type {};
  214. template <typename L, typename R>
  215. constexpr auto make_concat(L lhs, R rhs) -> concat<L, R> {
  216. return {lhs, rhs};
  217. }
  218. struct unknown_format {};
  219. template <typename Char>
  220. constexpr auto parse_text(basic_string_view<Char> str, size_t pos) -> size_t {
  221. for (size_t size = str.size(); pos != size; ++pos) {
  222. if (str[pos] == '{' || str[pos] == '}') break;
  223. }
  224. return pos;
  225. }
  226. template <typename Args, size_t POS, int ID, typename S>
  227. constexpr auto compile_format_string(S fmt);
  228. template <typename Args, size_t POS, int ID, typename T, typename S>
  229. constexpr auto parse_tail(T head, S fmt) {
  230. if constexpr (POS != basic_string_view<typename S::char_type>(fmt).size()) {
  231. constexpr auto tail = compile_format_string<Args, POS, ID>(fmt);
  232. if constexpr (std::is_same<remove_cvref_t<decltype(tail)>,
  233. unknown_format>())
  234. return tail;
  235. else
  236. return make_concat(head, tail);
  237. } else {
  238. return head;
  239. }
  240. }
  241. template <typename T, typename Char> struct parse_specs_result {
  242. formatter<T, Char> fmt;
  243. size_t end;
  244. int next_arg_id;
  245. };
  246. enum { manual_indexing_id = -1 };
  247. template <typename T, typename Char>
  248. constexpr auto parse_specs(basic_string_view<Char> str, size_t pos,
  249. int next_arg_id) -> parse_specs_result<T, Char> {
  250. str.remove_prefix(pos);
  251. auto ctx =
  252. compile_parse_context<Char>(str, max_value<int>(), nullptr, next_arg_id);
  253. auto f = formatter<T, Char>();
  254. auto end = f.parse(ctx);
  255. return {f, pos + fmt::detail::to_unsigned(end - str.data()),
  256. next_arg_id == 0 ? manual_indexing_id : ctx.next_arg_id()};
  257. }
  258. template <typename Char> struct arg_id_handler {
  259. arg_id_kind kind;
  260. arg_ref<Char> arg_id;
  261. constexpr auto on_auto() -> int {
  262. FMT_ASSERT(false, "handler cannot be used with automatic indexing");
  263. return 0;
  264. }
  265. constexpr auto on_index(int id) -> int {
  266. kind = arg_id_kind::index;
  267. arg_id = arg_ref<Char>(id);
  268. return 0;
  269. }
  270. constexpr auto on_name(basic_string_view<Char> id) -> int {
  271. kind = arg_id_kind::name;
  272. arg_id = arg_ref<Char>(id);
  273. return 0;
  274. }
  275. };
  276. template <typename Char> struct parse_arg_id_result {
  277. arg_id_kind kind;
  278. arg_ref<Char> arg_id;
  279. const Char* arg_id_end;
  280. };
  281. template <int ID, typename Char>
  282. constexpr auto parse_arg_id(const Char* begin, const Char* end) {
  283. auto handler = arg_id_handler<Char>{arg_id_kind::none, arg_ref<Char>{}};
  284. auto arg_id_end = parse_arg_id(begin, end, handler);
  285. return parse_arg_id_result<Char>{handler.kind, handler.arg_id, arg_id_end};
  286. }
  287. template <typename T, typename Enable = void> struct field_type {
  288. using type = remove_cvref_t<T>;
  289. };
  290. template <typename T>
  291. struct field_type<T, enable_if_t<detail::is_named_arg<T>::value>> {
  292. using type = remove_cvref_t<decltype(T::value)>;
  293. };
  294. template <typename T, typename Args, size_t END_POS, int ARG_INDEX, int NEXT_ID,
  295. typename S>
  296. constexpr auto parse_replacement_field_then_tail(S fmt) {
  297. using char_type = typename S::char_type;
  298. constexpr auto str = basic_string_view<char_type>(fmt);
  299. constexpr char_type c = END_POS != str.size() ? str[END_POS] : char_type();
  300. if constexpr (c == '}') {
  301. return parse_tail<Args, END_POS + 1, NEXT_ID>(
  302. field<char_type, typename field_type<T>::type, ARG_INDEX>(), fmt);
  303. } else if constexpr (c != ':') {
  304. FMT_THROW(format_error("expected ':'"));
  305. } else {
  306. constexpr auto result = parse_specs<typename field_type<T>::type>(
  307. str, END_POS + 1, NEXT_ID == manual_indexing_id ? 0 : NEXT_ID);
  308. if constexpr (result.end >= str.size() || str[result.end] != '}') {
  309. FMT_THROW(format_error("expected '}'"));
  310. return 0;
  311. } else {
  312. return parse_tail<Args, result.end + 1, result.next_arg_id>(
  313. spec_field<char_type, typename field_type<T>::type, ARG_INDEX>{
  314. result.fmt},
  315. fmt);
  316. }
  317. }
  318. }
  319. // Compiles a non-empty format string and returns the compiled representation
  320. // or unknown_format() on unrecognized input.
  321. template <typename Args, size_t POS, int ID, typename S>
  322. constexpr auto compile_format_string(S fmt) {
  323. using char_type = typename S::char_type;
  324. constexpr auto str = basic_string_view<char_type>(fmt);
  325. if constexpr (str[POS] == '{') {
  326. if constexpr (POS + 1 == str.size())
  327. FMT_THROW(format_error("unmatched '{' in format string"));
  328. if constexpr (str[POS + 1] == '{') {
  329. return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), fmt);
  330. } else if constexpr (str[POS + 1] == '}' || str[POS + 1] == ':') {
  331. static_assert(ID != manual_indexing_id,
  332. "cannot switch from manual to automatic argument indexing");
  333. constexpr auto next_id =
  334. ID != manual_indexing_id ? ID + 1 : manual_indexing_id;
  335. return parse_replacement_field_then_tail<get_type<ID, Args>, Args,
  336. POS + 1, ID, next_id>(fmt);
  337. } else {
  338. constexpr auto arg_id_result =
  339. parse_arg_id<ID>(str.data() + POS + 1, str.data() + str.size());
  340. constexpr auto arg_id_end_pos = arg_id_result.arg_id_end - str.data();
  341. constexpr char_type c =
  342. arg_id_end_pos != str.size() ? str[arg_id_end_pos] : char_type();
  343. static_assert(c == '}' || c == ':', "missing '}' in format string");
  344. if constexpr (arg_id_result.kind == arg_id_kind::index) {
  345. static_assert(
  346. ID == manual_indexing_id || ID == 0,
  347. "cannot switch from automatic to manual argument indexing");
  348. constexpr auto arg_index = arg_id_result.arg_id.index;
  349. return parse_replacement_field_then_tail<get_type<arg_index, Args>,
  350. Args, arg_id_end_pos,
  351. arg_index, manual_indexing_id>(
  352. fmt);
  353. } else if constexpr (arg_id_result.kind == arg_id_kind::name) {
  354. constexpr auto arg_index =
  355. get_arg_index_by_name(arg_id_result.arg_id.name, Args{});
  356. if constexpr (arg_index >= 0) {
  357. constexpr auto next_id =
  358. ID != manual_indexing_id ? ID + 1 : manual_indexing_id;
  359. return parse_replacement_field_then_tail<
  360. decltype(get_type<arg_index, Args>::value), Args, arg_id_end_pos,
  361. arg_index, next_id>(fmt);
  362. } else if constexpr (c == '}') {
  363. return parse_tail<Args, arg_id_end_pos + 1, ID>(
  364. runtime_named_field<char_type>{arg_id_result.arg_id.name}, fmt);
  365. } else if constexpr (c == ':') {
  366. return unknown_format(); // no type info for specs parsing
  367. }
  368. }
  369. }
  370. } else if constexpr (str[POS] == '}') {
  371. if constexpr (POS + 1 == str.size())
  372. FMT_THROW(format_error("unmatched '}' in format string"));
  373. return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), fmt);
  374. } else {
  375. constexpr auto end = parse_text(str, POS + 1);
  376. if constexpr (end - POS > 1) {
  377. return parse_tail<Args, end, ID>(make_text(str, POS, end - POS), fmt);
  378. } else {
  379. return parse_tail<Args, end, ID>(code_unit<char_type>{str[POS]}, fmt);
  380. }
  381. }
  382. }
  383. template <typename... Args, typename S,
  384. FMT_ENABLE_IF(is_compiled_string<S>::value)>
  385. constexpr auto compile(S fmt) {
  386. constexpr auto str = basic_string_view<typename S::char_type>(fmt);
  387. if constexpr (str.size() == 0) {
  388. return detail::make_text(str, 0, 0);
  389. } else {
  390. constexpr auto result =
  391. detail::compile_format_string<detail::type_list<Args...>, 0, 0>(fmt);
  392. return result;
  393. }
  394. }
  395. #endif // defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
  396. } // namespace detail
  397. FMT_BEGIN_EXPORT
  398. #if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
  399. template <typename CompiledFormat, typename... T,
  400. typename Char = typename CompiledFormat::char_type,
  401. FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
  402. FMT_INLINE FMT_CONSTEXPR_STRING auto format(const CompiledFormat& cf,
  403. const T&... args)
  404. -> std::basic_string<Char> {
  405. auto s = std::basic_string<Char>();
  406. cf.format(std::back_inserter(s), args...);
  407. return s;
  408. }
  409. template <typename OutputIt, typename CompiledFormat, typename... T,
  410. FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
  411. constexpr FMT_INLINE auto format_to(OutputIt out, const CompiledFormat& cf,
  412. const T&... args) -> OutputIt {
  413. return cf.format(out, args...);
  414. }
  415. template <typename S, typename... T,
  416. FMT_ENABLE_IF(is_compiled_string<S>::value)>
  417. FMT_INLINE FMT_CONSTEXPR_STRING auto format(const S&, T&&... args)
  418. -> std::basic_string<typename S::char_type> {
  419. if constexpr (std::is_same<typename S::char_type, char>::value) {
  420. constexpr auto str = basic_string_view<typename S::char_type>(S());
  421. if constexpr (str.size() == 2 && str[0] == '{' && str[1] == '}') {
  422. const auto& first = detail::first(args...);
  423. if constexpr (detail::is_named_arg<
  424. remove_cvref_t<decltype(first)>>::value) {
  425. return fmt::to_string(first.value);
  426. } else {
  427. return fmt::to_string(first);
  428. }
  429. }
  430. }
  431. constexpr auto compiled = detail::compile<T...>(S());
  432. if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
  433. detail::unknown_format>()) {
  434. return fmt::format(
  435. static_cast<basic_string_view<typename S::char_type>>(S()),
  436. std::forward<T>(args)...);
  437. } else {
  438. return fmt::format(compiled, std::forward<T>(args)...);
  439. }
  440. }
  441. template <typename OutputIt, typename S, typename... T,
  442. FMT_ENABLE_IF(is_compiled_string<S>::value)>
  443. FMT_CONSTEXPR auto format_to(OutputIt out, const S&, T&&... args) -> OutputIt {
  444. constexpr auto compiled = detail::compile<T...>(S());
  445. if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
  446. detail::unknown_format>()) {
  447. return fmt::format_to(
  448. out, static_cast<basic_string_view<typename S::char_type>>(S()),
  449. std::forward<T>(args)...);
  450. } else {
  451. return fmt::format_to(out, compiled, std::forward<T>(args)...);
  452. }
  453. }
  454. #endif
  455. template <typename OutputIt, typename S, typename... T,
  456. FMT_ENABLE_IF(is_compiled_string<S>::value)>
  457. auto format_to_n(OutputIt out, size_t n, const S& fmt, T&&... args)
  458. -> format_to_n_result<OutputIt> {
  459. using traits = detail::fixed_buffer_traits;
  460. auto buf = detail::iterator_buffer<OutputIt, char, traits>(out, n);
  461. fmt::format_to(std::back_inserter(buf), fmt, std::forward<T>(args)...);
  462. return {buf.out(), buf.count()};
  463. }
  464. template <typename S, typename... T,
  465. FMT_ENABLE_IF(is_compiled_string<S>::value)>
  466. FMT_CONSTEXPR20 auto formatted_size(const S& fmt, T&&... args) -> size_t {
  467. auto buf = detail::counting_buffer<>();
  468. fmt::format_to(appender(buf), fmt, std::forward<T>(args)...);
  469. return buf.count();
  470. }
  471. template <typename S, typename... T,
  472. FMT_ENABLE_IF(is_compiled_string<S>::value)>
  473. void print(std::FILE* f, const S& fmt, T&&... args) {
  474. auto buf = memory_buffer();
  475. fmt::format_to(appender(buf), fmt, std::forward<T>(args)...);
  476. detail::print(f, {buf.data(), buf.size()});
  477. }
  478. template <typename S, typename... T,
  479. FMT_ENABLE_IF(is_compiled_string<S>::value)>
  480. void print(const S& fmt, T&&... args) {
  481. print(stdout, fmt, std::forward<T>(args)...);
  482. }
  483. template <size_t N> class static_format_result {
  484. private:
  485. char data[N];
  486. public:
  487. template <typename S, typename... T,
  488. FMT_ENABLE_IF(is_compiled_string<S>::value)>
  489. explicit FMT_CONSTEXPR static_format_result(const S& fmt, T&&... args) {
  490. *fmt::format_to(data, fmt, std::forward<T>(args)...) = '\0';
  491. }
  492. auto str() const -> fmt::string_view { return {data, N - 1}; }
  493. auto c_str() const -> const char* { return data; }
  494. };
  495. /**
  496. * Formats arguments according to the format string `fmt_str` and produces
  497. * a string of the exact required size at compile time. Both the format string
  498. * and the arguments must be compile-time expressions.
  499. *
  500. * The resulting string can be accessed as a C string via `c_str()` or as
  501. * a `fmt::string_view` via `str()`.
  502. *
  503. * **Example**:
  504. *
  505. * // Produces the static string "42" at compile time.
  506. * static constexpr auto result = FMT_STATIC_FORMAT("{}", 42);
  507. * const char* s = result.c_str();
  508. */
  509. #define FMT_STATIC_FORMAT(fmt_str, ...) \
  510. fmt::static_format_result< \
  511. fmt::formatted_size(FMT_COMPILE(fmt_str), __VA_ARGS__) + 1>( \
  512. FMT_COMPILE(fmt_str), __VA_ARGS__)
  513. FMT_END_EXPORT
  514. FMT_END_NAMESPACE
  515. #endif // FMT_COMPILE_H_
  516. #else
  517. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  518. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)