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

Calling vsprintf with a vector instead of an explicit argument list #1244

Closed
delltom opened this issue Jul 22, 2019 · 1 comment
Closed

Calling vsprintf with a vector instead of an explicit argument list #1244

delltom opened this issue Jul 22, 2019 · 1 comment

Comments

@delltom
Copy link

delltom commented Jul 22, 2019

I have seen that the newest release of fmt allows for the following line of code:
fmt::vsprintf("%d %d", fmt::make_printf_args(42, 43));

I am attempting to do something similar by calling vsprintf like so:
fmt::vsprintf("%d %d", fmt::make_printf_args(vector<int> foobar))

or ideally:
fmt::vsprintf("%d %d", vector<int> foobar)

I have seen that functionality exists to call fmt::vformat() by creating your own argument vector using a similar workflow to that shown below, but have not been able to find a solution for vsprintf (see #819) :

using ctx = fmt::printf_context;
            std::vector<fmt::basic_format_args<ctx>> args;
            args.emplace_back(fmt::internal::make_arg<ctx>(42));
            args.emplace_back(fmt::internal::make_arg<ctx>("abc1"));
            args.emplace_back(fmt::internal::make_arg<ctx>(1.2f));
@delltom
Copy link
Author

delltom commented Jul 22, 2019

For anyone struggling with this. I got this to work after seeing the following comment. This is the code that worked for me:

std::vector<fmt::basic_format_arg<fmt::printf_context>> args;

args.push_back(fmt::internal::make_arg<fmt::printf_context>(42));
args.push_back(fmt::internal::make_arg<fmt::printf_context>(432.2));
args.push_back(fmt::internal::make_arg<fmt::printf_context>("hi"));

string a = fmt::vsprintf("%i, %f, %s", fmt::printf_args(args.data(), static_cast<unsigned>(args.size())));

This also worked when pushing args using a for loop.

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

1 participant