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

<format>: Cannot format a long value with formatter #2765

Closed
cpplearner opened this issue Jun 7, 2022 · 1 comment · Fixed by #2768
Closed

<format>: Cannot format a long value with formatter #2765

cpplearner opened this issue Jun 7, 2022 · 1 comment · Fixed by #2768
Labels
bug Something isn't working fixed Something works now, yay! format C++20/23 format

Comments

@cpplearner
Copy link
Contributor

Describe the bug
From https://stackoverflow.com/questions/72510490/stdformat-with-custom-types

Formatting a long value with formatter fails because long is not convertible to basic_format_arg.

With int it works as expected.

Command-line test case

D:\>type repro.cpp
#include <format>
#include <iostream>

template<class T>
struct Box {
    T value;
};

template<class T, class CharT>
struct std::formatter<Box<T>, CharT> : std::formatter<T, CharT> {
    template<class FormatContext>
    auto format(Box<T> t, FormatContext& fc) const {
        return std::formatter<T, CharT>::format(t.value, fc);
    }
};

int main() {
    Box<long> v = { 42 };
    std::cout << std::format("{:#x}", v);
}


D:\>cl /std:c++20 /EHsc repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.33.31424 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

repro.cpp
D:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.33.31424\include\format(2900): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'std::basic_format_arg<_Context>'
        with
        [
            _Context=std::format_context
        ]
D:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.33.31424\include\format(2900): note: No constructor could take the source type, or constructor overload resolution was ambiguous
repro.cpp(13): note: see reference to function template instantiation 'std::back_insert_iterator<std::_Fmt_buffer<char>> std::_Formatter_base<long,_CharT,std::_Basic_format_arg_type::_Int_type>::format<FormatContext>(const _Ty &,_FormatContext &) const' being compiled
        with
        [
            _CharT=char,
            FormatContext=std::format_context,
            _Ty=long,
            _FormatContext=std::format_context
        ]
repro.cpp(13): note: see reference to function template instantiation 'std::back_insert_iterator<std::_Fmt_buffer<char>> std::_Formatter_base<long,_CharT,std::_Basic_format_arg_type::_Int_type>::format<FormatContext>(const _Ty &,_FormatContext &) const' being compiled
        with
        [
            _CharT=char,
            FormatContext=std::format_context,
            _Ty=long,
            _FormatContext=std::format_context
        ]
D:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.33.31424\include\format(173): note: see reference to function template instantiation 'std::back_insert_iterator<std::_Fmt_buffer<char>> std::formatter<Box<long>,_CharT>::format<_Context>(Box<long>,FormatContext &) const' being compiled
        with
        [
            _CharT=char,
            _Context=std::format_context,
            FormatContext=std::format_context
        ]
D:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.33.31424\include\format(1449): note: see reference to variable template 'bool _Has_formatter<Box<long> &,std::basic_format_context<std::back_insert_iterator<std::_Fmt_buffer<char> >,char> >' being compiled
D:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.33.31424\include\format(1499): note: see reference to alias template instantiation 'std::_Format_arg_traits<_Context>::_Storage_type<_Ty>' being compiled
        with
        [
            _Context=std::format_context,
            _Ty=Box<long> &
        ]
D:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.33.31424\include\format(1534): note: see reference to variable template 'const size_t std::_Format_arg_traits<std::basic_format_context<std::back_insert_iterator<std::_Fmt_buffer<char> >,char> >::_Storage_size<Box<long> &>' being compiled
D:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.33.31424\include\format(2980): note: see reference to class template instantiation 'std::_Format_arg_store<std::format_context,Box<long> &>' being compiled
D:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.33.31424\include\format(3098): note: see reference to function template instantiation 'std::_Format_arg_store<std::format_context,Box<long> &> std::make_format_args<std::format_context,Box<long>&>(Box<long> &)' being compiled
repro.cpp(19): note: see reference to function template instantiation 'std::string std::format<Box<long>&>(const std::_Basic_format_string<char,Box<long> &>,Box<long> &)' being compiled
D:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.33.31424\include\format(2897): error C2672: 'std::visit_format_arg': no matching overloaded function found
D:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.33.31424\include\format(2900): error C2780: 'decltype(auto) std::visit_format_arg(_Visitor &&,std::basic_format_arg<_Context>)': expects 2 arguments - 1 provided
D:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.33.31424\include\format(349): note: see declaration of 'std::visit_format_arg'

Expected behavior
Should compile

STL version

Microsoft Visual Studio Community 2022 Preview
Version 17.3.0 Preview 1.0
@cpplearner
Copy link
Contributor Author

The culprit is

basic_format_arg<_FormatContext>{_Val});

the argument _Val should probably be cast to _Format_arg_traits<_FormatContext>::_Storage_type<_Ty>.

@StephanTLavavej StephanTLavavej added bug Something isn't working format C++20/23 format labels Jun 7, 2022
@StephanTLavavej StephanTLavavej added the fixed Something works now, yay! label Jun 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Something works now, yay! format C++20/23 format
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants