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

Format to char* works for built-in types, but not user types #793

Closed
wkenyon opened this issue Jun 29, 2018 · 5 comments
Closed

Format to char* works for built-in types, but not user types #793

wkenyon opened this issue Jun 29, 2018 · 5 comments

Comments

@wkenyon
Copy link

wkenyon commented Jun 29, 2018

#include <fmt/format.h>

enum class ExampleEnum : char
{
    A = '0',
    B = '1',
    C = '3',
    D = '?'
};


template <>
struct fmt::formatter<ExampleEnum> : fmt::formatter<fmt::string_view>
{
    using base = fmt::formatter<fmt::string_view>;

    template <typename FormatContext>
    auto format(const ExampleEnum& e, FormatContext& ctx)
        {
            switch (e)
            {
            case ExampleEnum::A:
                return base::format("A", ctx);
            case ExampleEnum::B:
                return base::format("B", ctx);
            case ExampleEnum::C:
                return base::format("C", ctx);
            case ExampleEnum::D:
                return base::format("D", ctx);
            };
            return base::format("NOT_RECOGNISED", ctx);
        }
};

int main() {
    char buf[80];
    memset(buf, 0, sizeof(buf));
    fmt::format_to(buf, "1: {}\n", "A");
    fmt::print(buf);

    char buf2[80];
    memset(buf2, 0, sizeof(buf2));
    fmt::format_to(buf2, "2: {}\n", ExampleEnum::A);
    fmt::print(buf2);

    fmt::print("3: {}\n", ExampleEnum::A);
    return 0;
}

As you can see, I'm trying to use format_to to write to a raw pointer. I know that you support 'iterators'. But a raw pointer is an iterator so this should work right? It seems to work for built-in types, but not this user defined formatter that inherits from string_view, as suggested in #787. I tested this on the latest master.

$ clang++ --std=c++14 -isystem external/fmt/include/  FmtBug.cpp -L bazel-bin/third_party/ -lfmt_lib
$ ./a.out
1: A
2: 
3: A
@eliaskosunen
Copy link
Contributor

eliaskosunen commented Jun 29, 2018

Using format_to inside the format member function fixes this for me:

    template <typename FormatContext>
    auto format(const ExampleEnum& e, FormatContext& ctx)
        {
            switch (e)
            {
            case ExampleEnum::A:
                return format_to(ctx.begin(), "A");
            case ExampleEnum::B:
                return format_to(ctx.begin(), "B");
            case ExampleEnum::C:
                return format_to(ctx.begin(), "C");
            case ExampleEnum::D:
                return format_to(ctx.begin(), "D");
            };
            return format_to(ctx.begin(), "NOT_RECOGNISED");
        }

I'm not sure if this could cause some issues, though.

@wkenyon
Copy link
Author

wkenyon commented Jun 29, 2018

Yes, thank you, however, that isn't an option for me because I want padding and alignment to work like the formatter for string_view.

@wkenyon
Copy link
Author

wkenyon commented Jun 29, 2018

If I just use format_to as you suggest, then padding and alignment are ignored.

@vitaut
Copy link
Contributor

vitaut commented Jul 4, 2018

Fixed in c04fb91. Thanks for reporting!

@vitaut vitaut closed this as completed Jul 4, 2018
@wkenyon
Copy link
Author

wkenyon commented Jul 5, 2018

Thanks,

Very kind

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

3 participants