-
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
Feature Request: support for manual creation of ArgList #528
Comments
I second this - This is a much needed feature |
@gabime : Glad you like the idea. This happened while I was on an effort to put spdlog behind an interface (that I want to use with boost:dll) I have added support for named arguments. I am willing to write up a pull request. |
Is it similiar to this pr? |
I didn't check that deeply, but I don't think so as this was meant for fmtlib and would be a more developed version of the code above. Basically I want to move away from an existing logging framework but need to keep the existing loggers formatting for a transitional period. The formatting would be done before the logging, providing a nice separation of concerns. Basically this can already be done now, but not without dragging in fmtlib dependencies everywhere.
So I want to be able to write it like this
when the logger configured for fmtlib formatting and like this
when the logger is configured for printf formatting. With a formatter interface and minimal templates most of the work is done.
|
Thanks for the suggestion. One problem with this is the lifetime of arguments. Since std::string s;
ArgBuilder args;
args.append(s + "a");
... Do you have any ideas how to address this? |
Prevent rvalues when calling the builder? You can still shoot yourself in the foot but not as easily. |
I'm not sure whether this is better than preventing, but one could just hold on to the temporaries.
but that leads to an ambiguous overload ....
so another overload is needed to disambiguate
|
@tnovotny The new experimental version of the library located in the std branch has a public API for constructing an argument list: auto args = fmt::make_args(42, "foo");
fmt::vformat("{}{}", args); The only difference is that you should pass all arguments at once. It is described in the standards proposal http://fmtlib.net/Text%20Formatting.html. |
That certainly helps, but would force me to use Still, if the arguments are already in another container, it would be very beneficial to have a way to convert them. Therefore an interface where not all the arguments are passed variadically would be helpful, which is why I went for the builder approach. But something like this would also work.
|
As mentioned in #590 (comment), I moved the core API that deals with argument lists to a separate header, |
@vitaut Thanx for the info. I'll have a look at it. |
Just as a proof of concept I tried the following (only works for < 16 args) ..
which can be used if we add another constructor to
to enable something like a builder in this manner:
Would that be a direction you would be willing to go? |
The proposed API looks fine to me, but I'd decouple it from template <typename Store>
basic_format_args(Store &store)
: types_(store.types()) {
set_data(store.data());
} and placing |
The auto args = fmt::make_args('c', 42);
fmt::vprint("{} {}", args); However, if you want to capture arguments and pass them to a formatting function later, you can just use #include <tuple>
#include "fmt/format.h"
template <typename... Args>
void print(const char* format_str, const std::tuple<Args...>& t) {
std::apply([=](const auto&... args) { fmt::print(format_str, args...); }, t);
}
int main() {
auto args = std::make_tuple('c', 42);
print("{} {}", args);
} |
@vitaut I had another look and the current state of fmtlib. I'm slightly confused, do you mean
still requires an internal type, so a corresponding |
Yes, the function was renamed per C++ standardization committee feedback. As for the printf context, a PR to add a type alias is welcome. |
Have a question to this issue. Its not realy clear for me how to use this. But I still not understand how to format like this
|
Well, you'd need to do something like this as
|
Ist not my Intension to do this manualy. Better Example:
|
Well, AFAIK there is no other way ATM. See also here: The dynamic |
I need a formatter interface. I would like to use
fmtlib
as one of the formatters. Maybe I missed something, but I was unable to find a way to build up anArgList
programmatically without accessing internals offmtlib
. Here is the general idea of what I am trying to accomplish:With the help of the
ArgBuilder
code likecan be written as
The text was updated successfully, but these errors were encountered: