Skip to content

Commit

Permalink
fix format a long value with formatter (#2768)
Browse files Browse the repository at this point in the history
Co-authored-by: S. B. Tam <[email protected]>
Co-authored-by: Stephan T. Lavavej <[email protected]>
  • Loading branch information
3 people authored Jun 12, 2022
1 parent 1c4166e commit 1385898
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
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) {}
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>)
-> 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

0 comments on commit 1385898

Please sign in to comment.