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

fmt() macro in format_to #783

Closed
eglimi opened this issue Jun 15, 2018 · 5 comments
Closed

fmt() macro in format_to #783

eglimi opened this issue Jun 15, 2018 · 5 comments

Comments

@eglimi
Copy link

eglimi commented Jun 15, 2018

The fmt() macro is very useful to check if arguments are valid. As far as I can see, this is only possible for format. E.g. (from the documentation):

std::string s = fmt::format(fmt("{:d}"), "foo"); // Produces compiler error

I would like to be able to use the macro also in format_to, in order to avoid allocations from std::string. For example

fmt::memory_buffer buf;
fmt::format_to(buf, fmt("{:d}"), "foo");

Is this planned, or do I miss something?

Thank you for your support and the great work on fmtlib!

@vitaut
Copy link
Contributor

vitaut commented Jun 24, 2018

Is this planned, or do I miss something?

It is planned, I just need to figure out how to do it in a clean way without proliferation of overloads.

@Milerius
Copy link

Milerius commented Jul 20, 2018

capture d ecran 2018-07-20 a 23 07 09

<3.

But let me tell you, there is worst from a microsoft library:

// The 'U' macro can be used to create a string or character literal of the platform type, i.e. utility::char_t.
// If you are using a library causing conflicts with 'U' macro, it can be turned off by defining the macro
// '_TURN_OFF_PLATFORM_STRING' before including the C++ REST SDK header files, and e.g. use '_XPLATSTR' instead.
#define U(x) _XPLATSTR(x)

use an internal token like underscore or something so people can name a variable fmt, or a member !

@vitaut
Copy link
Contributor

vitaut commented Jul 20, 2018

@Milerius, you can easily disable the fmt macro by defining FMT_NO_FMT_STRING_ALIAS.

@Milerius
Copy link

@vitaut Thank's !

@vitaut
Copy link
Contributor

vitaut commented Jul 22, 2018

Fixed in ffe414c. Now your example produces an expected compile-time error:

include/fmt/format.h:2312:27: error: constexpr variable 'invalid_format' must be initialized by a constant expression
  FMT_CONSTEXPR_DECL bool invalid_format =
                          ^
include/fmt/format.h:3532:13: note: in instantiation of function template specialization 'fmt::v5::internal::check_format_string<char [4], S>' requested here
  internal::check_format_string<Args...>(format_str);
            ^
test.cc:5:6: note: in instantiation of function template specialization 'fmt::v5::format_to<S, char [4], 500>' requested here
fmt::format_to(buf, fmt("{:d}"), "foo");
     ^
include/fmt/format.h:1418:13: note: non-constexpr function 'on_error' cannot be used in a constant expression
    handler.on_error("invalid type specifier");
            ^

Also refactored the code so that it's possible to handle both compile-time and dynamically checked format strings in one function template.

@vitaut vitaut closed this as completed Jul 22, 2018
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