-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
User-defined literals for format and named arguments #204
Conversation
Thanks for the contribution! This looks like a cool even if a bit unconventional use of user-defined literals. I'd like to experiment with this a bit more before merging in, hopefully in the beginning of the next week when I return from the trip I'm currently in. |
User-defined literals for format and named arguments
@dean0x7d, I get some warnings and errors on GCC 4.8.4 after merging this:
Do you know what might be causing these? |
I've temporarily moved the changes to the udl branch until the above issue is resolved. |
This is really strange. I tested this originally with GCC 4.8.4, Clang 3.7 and VS2015. I just tried again with GCC 4.8.4 and I can't reproduce the error. Everything is working fine. Is it possible that it's some system/library specific preprocessor definition messing with |
Interestingly, the error only occurs in tests. I can compile and run a small standalone example successfully: #include "format.h"
using namespace fmt::literals;
int main() {
fmt::print("The answer is {answer}.", "answer"_a=42);
} And |
Looks like a preprocessor problem, because changing the test to std::string str = "{}c{}"_format("ab", 1);
EXPECT_EQ(format("{}c{}", "ab", 1), str); compiles. |
The gtest macros pass their arguments both as code and as strings using the macro stringize operator (#). It's possible that's where it trips over the literal syntax. But that's still pretty weird. I haven't been able to reproduce the error either in tests or standalone. I tried with GCC 4.8.4 and 5.2.0. |
Yeah, looks like a stringification issue. The following code #define stringify(x) #x
stringify("{}c{}"_format("ab", 1)); is preprocessed to ""{}c{}"_format(\"ab\", 1)"; Note that the double quotes in Here's a relevant bug report: https://gcc.gnu.org/ml/gcc-bugs/2015-04/msg02027.html I guess a simple solution is to assign the result of |
I made the required changes to the tests. I haven't been able to reproduce the stringification bug on my end, so I can't actually test the workaround but I think this should be fine. Is there any way to add the relevant commit to this pull request or should I open a new one? |
Normally the PR is updated automatically when you push to correspondent branch, but I guess not for merged PRs. So I just pulled the commit from your branch. As expected the tests compile correctly now (why the issue only reproduces on my machine is a mystery). Thanks! |
@dean0x7d BTW, could you by any chance add some docs describing the new functionality? |
This is a bit of fun with C++11 user-defined literals. This brings C++ Format a little closer to Python's
str.format
for literals:The literal
_format
just passes the arguments tofmt::format
and is equivalent tofmt::format("{0}{1}{0}", "abra", "cad");
Literals can be used with named arguments as well:
The
_a
literal just constructs afmt::arg
under the hood.Compiler support for UDLs is required. Minimum Clang 3.1, GCC 4.7 or VS2015.
To use the literals
using namespace fmt::literals;
will import just_format
and_a
. Butusing namespace fmt;
is also a possibility.