Code Monkey home page Code Monkey logo

Comments (6)

vitaut avatar vitaut commented on May 21, 2024

This is a very good idea. The implementation can be substantially simplified though: see https://github.com/vitaut/format/blob/master/format-test.cc#L1490 - it only needs to be parameterized on character type. What do you think?
Again, sorry that it took me so long to reply.

from fmt.

jdale88 avatar jdale88 commented on May 21, 2024

Ah, I didn't realise a formatter could already take an initialisation list (I'm not sure that was available in the version I was using).

Yes, I agree, that is much simpler :)

So this would basically only need to add the code to work out if variadic templates were supported, and if so, add some extra Format functions like so?

#if FMT_USE_VARIADIC_TEMPLATES
template<typename... Args>
std::string Format(const StringRef &format, const Args & ... args) {
  Writer w;
  BasicFormatter<char> f(w, format.c_str(), { args... });
  return fmt::str(f);
}

template<typename... Args>
std::wstring Format(const WStringRef &format, const Args & ... args) {
  WWriter w;
  BasicFormatter<wchar_t> f(w, format.c_str(), { args... });
  return fmt::str(f);
}
#endif

Would you like me to roll that into a pull-request?

EDIT:
To be on the extra-safe side, it should probably also test for FMT_USE_INITIALIZER_LIST as well.

from fmt.

vitaut avatar vitaut commented on May 21, 2024

Exactly, it could be even simplified a little bit further by parameterizing on character type:

template<typename Char, typename... Args>
std::basic_string<Char> Format(const BasicStringRef<Char> ...);

or something like that.

Sure, feel free to submit a pull request and thanks for working on this.

from fmt.

jdale88 avatar jdale88 commented on May 21, 2024

I had thought about templating it further like that, but it saves little in the way of code duplication and adds extra syntax to the calling code to specify the char type:

template<typename Char, typename... Args>
std::basic_string<Char> Format(const BasicStringRef<Char> &format, const Args & ... args) {
  BasicWriter<Char> w;
  BasicFormatter<Char> f(w, format.c_str(), { args... });
  return fmt::str(f);
}

// ...

std::string str2 = fmt::Format<char>("Hello {0}", "World");
std::wstring wstr2 = fmt::Format<wchar_t>(L"Hello {0}", L"World");

Based on that, I feel having the two separate functions is cleaner.

I'll try and take a look at this shortly.

from fmt.

vitaut avatar vitaut commented on May 21, 2024

Making the calling code more complicated is not worth it of course, but isn't the Char type deduced from the first argument?

from fmt.

vitaut avatar vitaut commented on May 21, 2024

Ah, it doesn't. Two overloads (one for char and another for wchar_t) is fine then.

from fmt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.