Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting long with FMT_BUILTIN_TYPES=0 #4394

Open
montekarlos opened this issue Mar 21, 2025 · 3 comments
Open

Formatting long with FMT_BUILTIN_TYPES=0 #4394

montekarlos opened this issue Mar 21, 2025 · 3 comments

Comments

@montekarlos
Copy link

Consider the following code sample using fmtlib v11.1.4 or master

#define FMT_BUILTIN_TYPES 0

#include <cstdint>
#include <fmt/format.h>

int main() {
    long n(1234);
    fmt::print("1234!={}", n);    
}

The expected output of this is 1234!=1234 however when built and run using FMT_BUILTIN_TYPES=0 the results are 1234!=536895556 (your results will vary)

Note that changing long to int will resolve this

I'm hitting this issue on non-eabi (defines int32_t as long) but I can also create on x64 MSVC.

My understand is of this is that during argument processing custom.value and custom.format are set, but then basic_format_args::type detects as detail::type::int_type so then during formatting (visit) the wrong member of the union is used.

@vitaut
Copy link
Contributor

vitaut commented Mar 21, 2025

Could you provide a godbolt repro?

@montekarlos
Copy link
Author

Sadly godbolt doesn't provide libfmt library support for msvc and unknown-eabi fails linking fmtlib

https://godbolt.org/z/s7ba4Wvf3

@montekarlos
Copy link
Author

I don't fully understand what is happening

However when FMT_BUILTIN_TYPES=0 I see this behaviour

fmtlib v11.1.4:

base.1121:

using stored_type_constant = std::integral_constant<
    type, Context::builtin_types || TYPE == type::int_type ? TYPE
                                                           : type::custom_type>;

Is returning int_type even though

However base:2138
FMT_CONSTEXPR FMT_INLINE value(long x FMT_BUILTIN) : value(long_type(x)) {} has been disabled so will be compiled via

  template <typename T,
            FMT_ENABLE_IF(use_formatter<T>::value || !FMT_BUILTIN_TYPES)>
  FMT_CONSTEXPR20 FMT_INLINE value(T& x) : value(x, custom_tag()) {}

Which results in a custom with an integer type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants