Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 39 additions & 36 deletions stl/inc/format
Original file line number Diff line number Diff line change
Expand Up @@ -745,10 +745,45 @@ public:
}
}

private:
template <class _Visitor, class _Ctx>
friend decltype(auto) visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
template <class _Visitor>
decltype(auto) _Visit(_Visitor&& _Vis) {
switch (_Active_state) {
case _Basic_format_arg_type::_None:
return _STD forward<_Visitor>(_Vis)(_No_state);
case _Basic_format_arg_type::_Int_type:
return _STD forward<_Visitor>(_Vis)(_Int_state);
case _Basic_format_arg_type::_UInt_type:
return _STD forward<_Visitor>(_Vis)(_UInt_state);
case _Basic_format_arg_type::_Long_long_type:
return _STD forward<_Visitor>(_Vis)(_Long_long_state);
case _Basic_format_arg_type::_ULong_long_type:
return _STD forward<_Visitor>(_Vis)(_ULong_long_state);
case _Basic_format_arg_type::_Bool_type:
return _STD forward<_Visitor>(_Vis)(_Bool_state);
case _Basic_format_arg_type::_Char_type:
return _STD forward<_Visitor>(_Vis)(_Char_state);
case _Basic_format_arg_type::_Float_type:
return _STD forward<_Visitor>(_Vis)(_Float_state);
case _Basic_format_arg_type::_Double_type:
return _STD forward<_Visitor>(_Vis)(_Double_state);
case _Basic_format_arg_type::_Long_double_type:
return _STD forward<_Visitor>(_Vis)(_Long_double_state);
case _Basic_format_arg_type::_Pointer_type:
return _STD forward<_Visitor>(_Vis)(_Pointer_state);
case _Basic_format_arg_type::_CString_type:
return _STD forward<_Visitor>(_Vis)(_CString_state);
case _Basic_format_arg_type::_String_type:
return _STD forward<_Visitor>(_Vis)(_String_state);
case _Basic_format_arg_type::_Custom_type:
return _STD forward<_Visitor>(_Vis)(_Custom_state);
default:
_STL_VERIFY(false, "basic_format_arg is in impossible state");
int _Dummy{};
return _STD forward<_Visitor>(_Vis)(_Dummy);
}
}

private:
friend basic_format_args<_Context>;
friend _Format_handler<_CharType>;
friend _Format_arg_traits<_Context>;
Expand Down Expand Up @@ -838,39 +873,7 @@ auto _Format_arg_traits<_Context>::_Type_eraser() {

_EXPORT_STD template <class _Visitor, class _Context>
decltype(auto) visit_format_arg(_Visitor&& _Vis, basic_format_arg<_Context> _Arg) {
switch (_Arg._Active_state) {
case _Basic_format_arg_type::_None:
return _STD forward<_Visitor>(_Vis)(_Arg._No_state);
case _Basic_format_arg_type::_Int_type:
return _STD forward<_Visitor>(_Vis)(_Arg._Int_state);
case _Basic_format_arg_type::_UInt_type:
return _STD forward<_Visitor>(_Vis)(_Arg._UInt_state);
case _Basic_format_arg_type::_Long_long_type:
return _STD forward<_Visitor>(_Vis)(_Arg._Long_long_state);
case _Basic_format_arg_type::_ULong_long_type:
return _STD forward<_Visitor>(_Vis)(_Arg._ULong_long_state);
case _Basic_format_arg_type::_Bool_type:
return _STD forward<_Visitor>(_Vis)(_Arg._Bool_state);
case _Basic_format_arg_type::_Char_type:
return _STD forward<_Visitor>(_Vis)(_Arg._Char_state);
case _Basic_format_arg_type::_Float_type:
return _STD forward<_Visitor>(_Vis)(_Arg._Float_state);
case _Basic_format_arg_type::_Double_type:
return _STD forward<_Visitor>(_Vis)(_Arg._Double_state);
case _Basic_format_arg_type::_Long_double_type:
return _STD forward<_Visitor>(_Vis)(_Arg._Long_double_state);
case _Basic_format_arg_type::_Pointer_type:
return _STD forward<_Visitor>(_Vis)(_Arg._Pointer_state);
case _Basic_format_arg_type::_CString_type:
return _STD forward<_Visitor>(_Vis)(_Arg._CString_state);
case _Basic_format_arg_type::_String_type:
return _STD forward<_Visitor>(_Vis)(_Arg._String_state);
case _Basic_format_arg_type::_Custom_type:
return _STD forward<_Visitor>(_Vis)(_Arg._Custom_state);
default:
_STL_VERIFY(false, "basic_format_arg is in impossible state");
return _STD forward<_Visitor>(_Vis)(0);
}
return _Arg._Visit(_STD forward<_Visitor>(_Vis));
}

// we need to implement this ourselves because from_chars does not work with wide characters and isn't constexpr
Expand Down
16 changes: 16 additions & 0 deletions tests/std/tests/P0645R10_text_formatting_args/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,29 @@ void test_lwg3810() {
static_assert(same_as<decltype(basic_format_args{args_store}), basic_format_args<Context>>);
}

struct lvalue_only_visitor {
template <class T>
void operator()(T&&) const = delete;
template <class T>
void operator()(T&) const noexcept {}
};

template <class Context>
void test_lvalue_only_visitation() {
visit_format_arg(lvalue_only_visitor{}, basic_format_arg<Context>{});
}

int main() {
test_basic_format_arg<format_context>();
test_basic_format_arg<wformat_context>();
test_format_arg_store<format_context>();
test_format_arg_store<wformat_context>();
test_visit_monostate<format_context>();
test_visit_monostate<wformat_context>();

test_lwg3810<format_context>();
test_lwg3810<wformat_context>();

test_lvalue_only_visitation<format_context>();
test_lvalue_only_visitation<wformat_context>();
}