[libcxx-commits] [libcxx] [libc++] Replace `std::optional` in `__format/format_context.h` with a re-implementation (PR #148876)

William Tran-Viet via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 15 09:14:30 PDT 2025


smallp-o-p wrote:

> I don't think this is a good idea. Could you elaborate on where there is a circular dependency? AFAICT You only need to specialize two variables, which definitely doesn't have to cause circular dependencies.

`optional -> __format/range_default_formatter.h -> __format/range_formatter.h -> __format/format_context.h -> optional` 
^ What I need from `__format/range_default_formatter.h` is `std::range_format`. 

I absolutely agree that this isn't preferable. I'll keep this open for now and look for another way to get the `std::range_format`. Now that I post this, I think we can just cut that enum out into its own header....

But here's an example of where it's going wrong: 
```# .---command stderr------------
# | In file included from /home/willtv/smallpop-llvm-project/libcxx/test/libcxx/utilities/optional/optional.object/iterator.pass.cpp:23:
# | In file included from /home/willtv/smallpop-llvm-project/build-optional/libcxx/test-suite-install/include/c++/v1/optional:245:
# | In file included from /home/willtv/smallpop-llvm-project/build-optional/libcxx/test-suite-install/include/c++/v1/__format/range_default_formatter.h:23:
# | In file included from /home/willtv/smallpop-llvm-project/build-optional/libcxx/test-suite-install/include/c++/v1/__format/range_formatter.h:23:
# | /home/willtv/smallpop-llvm-project/build-optional/libcxx/test-suite-install/include/c++/v1/__format/format_context.h:57:25: error: no template named 'optional'
# |    57 |                         optional<std::locale>&& __loc = nullopt) {
# |       |                         ^
# | /home/willtv/smallpop-llvm-project/build-optional/libcxx/test-suite-install/include/c++/v1/__format/format_context.h:57:57: error: use of undeclared identifier 'nullopt'; did you mean 'nullptr'?
# |    57 |                         optional<std::locale>&& __loc = nullopt) {
# |       |                                                         ^~~~~~~
# |       |                                                         nullptr
# | /home/willtv/smallpop-llvm-project/build-optional/libcxx/test-suite-install/include/c++/v1/__format/format_context.h:57:57: error: address of overloaded function 'nullopt' does not match required type 'int'
# |    57 |                         optional<std::locale>&& __loc = nullopt) {
# |       |                                                         ^~~~~~~
# | /home/willtv/smallpop-llvm-project/build-optional/libcxx/test-suite-install/include/c++/v1/__format/format_context.h:57:49: note: passing argument to parameter '__loc' here
# |    57 |                         optional<std::locale>&& __loc = nullopt) {
# |       |                                                 ^
# | /home/willtv/smallpop-llvm-project/build-optional/libcxx/test-suite-install/include/c++/v1/__format/format_context.h:110:3: error: no template named 'optional'
# |   110 |   optional<std::locale> __loc_;
# |       |   ^
# | /home/willtv/smallpop-llvm-project/build-optional/libcxx/test-suite-install/include/c++/v1/__format/format_context.h:114:87: error: no template named 'optional'
# |   114 |       _OtherOutIt, basic_format_args<basic_format_context<_OtherOutIt, _OtherCharT>>, optional<std::locale>&&);
# |       |                                                                                       ^
# | /home/willtv/smallpop-llvm-project/build-optional/libcxx/test-suite-install/include/c++/v1/__format/format_context.h:118:72: error: no template named 'optional'
# |   118 |       _OutIt __out_it, basic_format_args<basic_format_context> __args, optional<std::locale>&& __loc)
# |       |                                                                        ^
# | 6 errors generated.
# `-----------------------------
# error: command failed with exit status: 1```

https://github.com/llvm/llvm-project/pull/148876


More information about the libcxx-commits mailing list