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

Formatting for derived classes #785

Closed
ukreator opened this issue Jun 19, 2018 · 2 comments
Closed

Formatting for derived classes #785

ukreator opened this issue Jun 19, 2018 · 2 comments

Comments

@ukreator
Copy link

ukreator commented Jun 19, 2018

Hi,

What 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:

#include <fmt/format.h>

namespace A
{
struct X {
    int x = 1;
};
struct Y: public X {
};
}

namespace fmt
{
template<>
struct formatter<A::X> {
   template<typename ParseContext>
   constexpr auto parse(ParseContext& ctx) {
       return ctx.begin();
   }

   template<typename FormatContext>
   auto format(const A::X& val, FormatContext& ctx) {
       return format_to(ctx.begin(), "{}", val.x);
   }
};
}

int main() {
    auto s = fmt::format("{}", A::Y{});
}

Compilation error:

In file included from <source>:1:
In file included from /opt/compiler-explorer/libs/fmt/trunk/include/fmt/format.h:54:
/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>'
    typename Context::template formatter_type<T>::type f;
                                                       ^
/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
    custom.format = &format_custom_arg<T>;
                     ^
/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
  FMT_CONSTEXPR typed_value(const T &val) : value<Context>(val) {}
                                            ^
/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
  return make_value<Context>(value);
         ^
/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
    : data_{internal::make_arg<IS_PACKED, Context>(args)...} {}
                      ^
/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
  format_arg_store<format_context, Args...> as{args...};
                                            ^
<source>:29:19: note: in instantiation of function template specialization 'fmt::v5::format<A::Y>' requested here
    auto s = fmt::format("{}", A::Y{});
                  ^
/opt/compiler-explorer/libs/fmt/trunk/include/fmt/core.h:315:8: note: template is declared here
struct formatter;
@ukreator
Copy link
Author

My colleague suggested to use something like this:

template<typename T, typename Char>
struct formatter<T, Char,  std::enable_if_t<std::is_convertible<T*, A::X*>::value>> {
    // ...
}

which seem to be working. Closing this issue.

@vitaut
Copy link
Contributor

vitaut commented Jun 19, 2018

My colleague suggested to use something like this

Yep, that's the way to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants