{"action":"created","issue":{"url":"https://api.github.com/repos/fmtlib/fmt/issues/785","repository_url":"https://api.github.com/repos/fmtlib/fmt","labels_url":"https://api.github.com/repos/fmtlib/fmt/issues/785/labels{/name}","comments_url":"https://api.github.com/repos/fmtlib/fmt/issues/785/comments","events_url":"https://api.github.com/repos/fmtlib/fmt/issues/785/events","html_url":"https://github.com/fmtlib/fmt/issues/785","id":333490742,"node_id":"MDU6SXNzdWUzMzM0OTA3NDI=","number":785,"title":"Formatting for derived classes","user":{"login":"ukreator","id":657287,"node_id":"MDQ6VXNlcjY1NzI4Nw==","avatar_url":"https://avatars2.githubusercontent.com/u/657287?v=4","gravatar_id":"","url":"https://api.github.com/users/ukreator","html_url":"https://github.com/ukreator","followers_url":"https://api.github.com/users/ukreator/followers","following_url":"https://api.github.com/users/ukreator/following{/other_user}","gists_url":"https://api.github.com/users/ukreator/gists{/gist_id}","starred_url":"https://api.github.com/users/ukreator/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ukreator/subscriptions","organizations_url":"https://api.github.com/users/ukreator/orgs","repos_url":"https://api.github.com/users/ukreator/repos","events_url":"https://api.github.com/users/ukreator/events{/privacy}","received_events_url":"https://api.github.com/users/ukreator/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-06-19T01:44:10Z","updated_at":"2018-06-19T09:27:43Z","closed_at":"2018-06-19T09:27:43Z","author_association":"NONE","body":"Hi,\r\n\r\nWhat is the correct way to provide the formatter for a derived class that uses the formatter of the base class? Something like this doesn't work:\r\n\r\n```c++\r\n#include <fmt/format.h>\r\n\r\nnamespace A\r\n{\r\nstruct X {\r\n    int x = 1;\r\n};\r\nstruct Y: public X {\r\n};\r\n}\r\n\r\nnamespace fmt\r\n{\r\ntemplate<>\r\nstruct formatter<A::X> {\r\n   template<typename ParseContext>\r\n   constexpr auto parse(ParseContext& ctx) {\r\n       return ctx.begin();\r\n   }\r\n\r\n   template<typename FormatContext>\r\n   auto format(const A::X& val, FormatContext& ctx) {\r\n       return format_to(ctx.begin(), \"{}\", val.x);\r\n   }\r\n};\r\n}\r\n\r\nint main() {\r\n    auto s = fmt::format(\"{}\", A::Y{});\r\n}\r\n```\r\n\r\nCompilation error:\r\n\r\n\r\n```\r\nIn file included from <source>:1:\r\nIn file included from /opt/compiler-explorer/libs/fmt/trunk/include/fmt/format.h:54:\r\n/opt/compiler-explorer/libs/fmt/trunk/include/fmt/core.h:541:56: error: implicit instantiation of undefined template 'fmt::v5::formatter<A::Y, char, void>'\r\n    typename Context::template formatter_type<T>::type f;\r\n                                                       ^\r\n/opt/compiler-explorer/libs/fmt/trunk/include/fmt/core.h:527:22: note: in instantiation of function template specialization 'fmt::v5::internal::value<fmt::v5::basic_format_context<std::back_insert_iterator<fmt::v5::internal::basic_buffer<char> >, char> >::format_custom_arg<A::Y>' requested here\r\n    custom.format = &format_custom_arg<T>;\r\n                     ^\r\n/opt/compiler-explorer/libs/fmt/trunk/include/fmt/core.h:553:45: note: in instantiation of function template specialization 'fmt::v5::internal::value<fmt::v5::basic_format_context<std::back_insert_iterator<fmt::v5::internal::basic_buffer<char> >, char> >::value<A::Y>' requested here\r\n  FMT_CONSTEXPR typed_value(const T &val) : value<Context>(val) {}\r\n                                            ^\r\n/opt/compiler-explorer/libs/fmt/trunk/include/fmt/core.h:960:10: note: in instantiation of function template specialization 'fmt::v5::internal::make_value<fmt::v5::basic_format_context<std::back_insert_iterator<fmt::v5::internal::basic_buffer<char> >, char>, A::Y, char>' requested here\r\n  return make_value<Context>(value);\r\n         ^\r\n/opt/compiler-explorer/libs/fmt/trunk/include/fmt/core.h:1013:23: note: in instantiation of function template specialization 'fmt::v5::internal::make_arg<true, fmt::v5::basic_format_context<std::back_insert_iterator<fmt::v5::internal::basic_buffer<char> >, char>, A::Y>' requested here\r\n    : data_{internal::make_arg<IS_PACKED, Context>(args)...} {}\r\n                      ^\r\n/opt/compiler-explorer/libs/fmt/trunk/include/fmt/core.h:1262:45: note: in instantiation of member function 'fmt::v5::format_arg_store<fmt::v5::basic_format_context<std::back_insert_iterator<fmt::v5::internal::basic_buffer<char> >, char>, A::Y>::format_arg_store' requested here\r\n  format_arg_store<format_context, Args...> as{args...};\r\n                                            ^\r\n<source>:29:19: note: in instantiation of function template specialization 'fmt::v5::format<A::Y>' requested here\r\n    auto s = fmt::format(\"{}\", A::Y{});\r\n                  ^\r\n/opt/compiler-explorer/libs/fmt/trunk/include/fmt/core.h:315:8: note: template is declared here\r\nstruct formatter;\r\n```"},"comment":{"url":"https://api.github.com/repos/fmtlib/fmt/issues/comments/398335646","html_url":"https://github.com/fmtlib/fmt/issues/785#issuecomment-398335646","issue_url":"https://api.github.com/repos/fmtlib/fmt/issues/785","id":398335646,"node_id":"MDEyOklzc3VlQ29tbWVudDM5ODMzNTY0Ng==","user":{"login":"ukreator","id":657287,"node_id":"MDQ6VXNlcjY1NzI4Nw==","avatar_url":"https://avatars2.githubusercontent.com/u/657287?v=4","gravatar_id":"","url":"https://api.github.com/users/ukreator","html_url":"https://github.com/ukreator","followers_url":"https://api.github.com/users/ukreator/followers","following_url":"https://api.github.com/users/ukreator/following{/other_user}","gists_url":"https://api.github.com/users/ukreator/gists{/gist_id}","starred_url":"https://api.github.com/users/ukreator/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ukreator/subscriptions","organizations_url":"https://api.github.com/users/ukreator/orgs","repos_url":"https://api.github.com/users/ukreator/repos","events_url":"https://api.github.com/users/ukreator/events{/privacy}","received_events_url":"https://api.github.com/users/ukreator/received_events","type":"User","site_admin":false},"created_at":"2018-06-19T09:27:43Z","updated_at":"2018-06-19T09:27:43Z","author_association":"NONE","body":"My colleague suggested to use something like this:\r\n```c++\r\ntemplate<typename T, typename Char>\r\nstruct formatter<T, Char,  std::enable_if_t<std::is_convertible<T*, A::X*>::value>> {\r\n    // ...\r\n}\r\n```\r\n\r\nwhich seem to be working. Closing this issue."}}
{"actor":{"display_login":"ukreator"}}