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

fix format a long value with formatter #2768

Merged
merged 9 commits into from
Jun 12, 2022
7 changes: 6 additions & 1 deletion stl/inc/format
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,10 @@ public:
: _Active_state(_Basic_format_arg_type::_Int_type), _Int_state(_Val) {}
explicit basic_format_arg(const unsigned int _Val) noexcept
: _Active_state(_Basic_format_arg_type::_UInt_type), _UInt_state(_Val) {}
explicit basic_format_arg(const long _Val) noexcept
: _Active_state(_Basic_format_arg_type::_Int_type), _Int_state(_Val) {}
explicit basic_format_arg(const unsigned long _Val) noexcept
: _Active_state(_Basic_format_arg_type::_UInt_type), _UInt_state(_Val) {}
barcharcraz marked this conversation as resolved.
Show resolved Hide resolved
explicit basic_format_arg(const long long _Val) noexcept
: _Active_state(_Basic_format_arg_type::_Long_long_type), _Long_long_state(_Val) {}
explicit basic_format_arg(const unsigned long long _Val) noexcept
Expand Down Expand Up @@ -1769,14 +1773,15 @@ struct _Format_arg_traits {
static auto _Phony_basic_format_arg_constructor(double) -> double; // not defined
static auto _Phony_basic_format_arg_constructor(long double) -> long double; // not defined

static auto _Phony_basic_format_arg_constructor(_Char_type*) -> const _Char_type*; // not defined
static auto _Phony_basic_format_arg_constructor(const _Char_type*) -> const _Char_type*; // not defined

template <class _Traits>
static auto _Phony_basic_format_arg_constructor(basic_string_view<_Char_type, _Traits>)
-> basic_string_view<_Char_type>; // not defined

template <class _Traits, class _Alloc>
static auto _Phony_basic_format_arg_constructor(const basic_string<_Char_type, _Traits, _Alloc>&)
static auto _Phony_basic_format_arg_constructor(basic_string<_Char_type, _Traits, _Alloc>)
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
-> basic_string_view<_Char_type>; // not defined

static auto _Phony_basic_format_arg_constructor(nullptr_t) -> const void*; // not defined
Expand Down
12 changes: 12 additions & 0 deletions tests/std/tests/P0645R10_text_formatting_args/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cstdint>
#include <format>
#include <memory>
#include <string>
#include <string_view>
#include <type_traits>
#include <variant>
Expand Down Expand Up @@ -212,6 +213,17 @@ void test_format_arg_store() {
static_assert(sizeof(_Format_arg_index) == sizeof(size_t));
static_assert(is_same_v<_Format_arg_traits<format_context>::_Storage_type<void*>, const void*>);

static_assert(is_same_v<_Format_arg_traits<format_context>::_Storage_type<string>, string_view>);
static_assert(is_same_v<_Format_arg_traits<format_context>::_Storage_type<const string>, string_view>);
static_assert(is_same_v<_Format_arg_traits<format_context>::_Storage_type<char*>, const char*>);
static_assert(is_same_v<_Format_arg_traits<format_context>::_Storage_type<const char*>, const char*>);

// we rely on the _Storage_type<long> to be int in:
// explicit basic_format_arg(const long _Val) noexcept
// : _Active_state(_Basic_format_arg_type::_Int_type), _Int_state(_Val) {}
static_assert(is_same_v<_Format_arg_traits<format_context>::_Storage_type<long>, int>);
static_assert(is_same_v<_Format_arg_traits<format_context>::_Storage_type<unsigned long>, unsigned int>);

template <class Context>
void test_visit_monostate() {
assert(visit_format_arg(visitor<Context>, basic_format_arg<Context>()) == Arg_type::none);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,29 +172,35 @@ void test_format_family_overloads() {
template <class charT>
void test_custom_formattable_type() {
test_numeric_custom_formattable_type<int, charT>();
test_numeric_custom_formattable_type<long, charT>();
test_numeric_custom_formattable_type<long long, charT>();
test_numeric_custom_formattable_type<unsigned int, charT>();
test_numeric_custom_formattable_type<unsigned long, charT>();
test_numeric_custom_formattable_type<unsigned long long, charT>();
test_numeric_custom_formattable_type<short, charT>();
#ifdef _NATIVE_WCHAR_T_DEFINED
test_numeric_custom_formattable_type<unsigned short, charT>();
#endif
test_numeric_custom_formattable_type<float, charT>();
test_numeric_custom_formattable_type<double, charT>();
test_numeric_custom_formattable_type<long double, charT>();
}

template <class charT>
void test_mixed_custom_formattable_type() {
test_numeric_mixed_args_custom_formattable_type<int, charT>();
test_numeric_mixed_args_custom_formattable_type<long, charT>();
test_numeric_mixed_args_custom_formattable_type<long long, charT>();
test_numeric_mixed_args_custom_formattable_type<unsigned int, charT>();
test_numeric_mixed_args_custom_formattable_type<unsigned long, charT>();
test_numeric_mixed_args_custom_formattable_type<unsigned long long, charT>();
test_numeric_mixed_args_custom_formattable_type<short, charT>();
#ifdef _NATIVE_WCHAR_T_DEFINED
test_numeric_mixed_args_custom_formattable_type<unsigned short, charT>();
#endif
test_numeric_mixed_args_custom_formattable_type<float, charT>();
test_numeric_mixed_args_custom_formattable_type<double, charT>();
test_numeric_mixed_args_custom_formattable_type<long double, charT>();
}

int main() {
Expand Down