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

Cannot pass named arg twice #1788

Closed
artemyv opened this issue Jul 21, 2020 · 2 comments
Closed

Cannot pass named arg twice #1788

artemyv opened this issue Jul 21, 2020 · 2 comments

Comments

@artemyv
Copy link

artemyv commented Jul 21, 2020

I have a template function that receives fmt::arg list and has to call 2 other functions each one calls the fmt::format as a result compilation error core.h(1653): error C2338: passing views as lvalues is disallowed occurs
Simplified example

`template <typename... Args>
inline std::string formatmock(const std::string& format_str, const std::string& format_str2, Args&&... args) {
fmt::format(format_str, (args)...) + fmt::format(format_str2,std::forward(args)...);
}

TEST(FmtArg, namedpasssimple)
{
std::string message = formatmock("The answer is {value}","The answer is {value}", fmt::arg("value", 42));
ASSERT_EQ(message, "The answer is 42The answer is 42");
}`

How can I pass named arguments twice?

@vitaut
Copy link
Contributor

vitaut commented Jul 21, 2020

You can do it as follows:

#include <fmt/core.h>

template <typename... Args>
std::string formatmock(const std::string& format_str,
                       const std::string& format_str2, Args&&... args) {
  return fmt::format(format_str, Args(args)...) +
         fmt::format(format_str2, Args(args)...);
}

int main() {
  formatmock("The answer is {value}", "The answer is {value}",
             fmt::arg("value", 42));
}

https://godbolt.org/z/7KKjzx

@vitaut vitaut closed this as completed Jul 21, 2020
@artemyv
Copy link
Author

artemyv commented Jul 22, 2020

Thank you, it is working

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

2 participants