Comments (6)
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.
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.
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.
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.
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.
Ah, it doesn't. Two overloads (one for char and another for wchar_t) is fine then.
from fmt.
Related Issues (20)
- std::chrono::current_zone() is not meant to return the local time zone HOT 4
- It would be good to have a customizable separator in fmt::group_digits(...) HOT 1
- fmt::styled and fmt::formatter<std::string> parse deactivate colors/termaps HOT 1
- formatting of variant types HOT 3
- FMT_USE_NONTYPE_TEMPLATE_ARGS detection is wrong on trunk HOT 2
- Support for a single `char` separator in `fmt::join()` HOT 1
- Formatting for strings with custom allocators fails to compile HOT 2
- Advice for extending libfmt for GPU support? HOT 2
- xchar.h extra dependencies HOT 1
- cannot use in CUDA (+MSVC) for newest branch master HOT 1
- Question: what's the difference between string-like formatters and where are they defined? HOT 2
- fmt won't let me specialize a formatter to format mixed character types. HOT 2
- `fmt::isnan` triggers floating-point exception for NaN values HOT 4
- Header-only mode not linking with fmt::ostream HOT 1
- g++-14 compilation error using named arguments inside a generic lambda HOT 2
- Add support for grouping numbers and '_', ',' separators HOT 1
- How do you forward fmt::format_string without explicitly indicating it in the interface? HOT 1
- Fail to build on Ubuntu-18.04 with g++-13.2 HOT 8
- format_as support in fmt/core.h HOT 1
- format_as but with iterator HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fmt.