Fmt: Could you support container output?

Created on 2 Sep 2017  路  3Comments  路  Source: fmtlib/fmt

Assume the syntax:

{[],<the_delim_to_join>}

saying that the corresponding argument is treated as a "container".

For example, the invocation

std::vector<std::string> choices = {"apple", "pear", "banana"};
fmt::printf("Your can choose {[], or}!", fmt::make_container_arg(choices.begin(), choices.end()))

give:

You can choose apple or pear or banana!

Most helpful comment

This is already supported with a slightly different syntax:

  std::vector<std::string> choices = {"apple", "pear", "banana"};
  fmt::print("Your can choose {}!", fmt::join(choices, " or "));

prints

Your can choose apple or pear or banana!

All 3 comments

This is already supported with a slightly different syntax:

  std::vector<std::string> choices = {"apple", "pear", "banana"};
  fmt::print("Your can choose {}!", fmt::join(choices, " or "));

prints

Your can choose apple or pear or banana!

I think it isn't what I sugguest.

Consider we want implement the i18n, we want print the prompt in different language. the program fragment

fmt::join(choices, " or "));

is not the best solution becase it hard code the "or" part.
If we read the "or" from the "configuration file", as:

const char* THE_OR  = /*read from config file*/;
fmt::join(choices, THE_OR));

It's elaborated and lack of the flexibility. In a word, "or" should be present alone with the setence "Your can choose {}".

There is no built-in way to specify separator in the format string, but you can easily write your own class similar to ArgJoin that takes a separator as part of the format string:

fmt::print("You can choose {: or }!", myjoin(choices));
Was this page helpful?
0 / 5 - 0 ratings