Comments (3)
Sounds like a useful and interesting challange. The tricky bits would be (1) gracefully degrading for compliers without constexpr
support, and for dynamic format strings, and (2) implementing it all without bloating up the codebase too much more. The first link gives me hope that it's actually quite simple.
from tinyformat.
I had a look at this. It's fairly easy (if cumbersome) to implement a constexpr function which counts the number of format specs in a string at compile time:
constexpr int countFormatSpecs(const char* str, int i, int N)
{
return
/*if*/ (i >= N-1) ? (
/*if*/ (str[i] != '%') ?
0
/*else*/ :
throw std::logic_error("Format string prematurely terminated with a '%'")
)
/*else*/ : (
/*if*/ (str[i] == '%') ? (
/*if*/ (str[i+1] == '%') ? (
countFormatSpecs(str, i+2, N)
)
/*else*/ : (
1 + countFormatSpecs(str, i+1, N)
)
)
/*else*/ : (
countFormatSpecs(str, i+1, N)
)
)
;
}
template<int N>
constexpr int countFormatSpecs(const char (&str)[N])
{
return countFormatSpecs(str, 0, N-1);
}
Unfortunately this was about as far as I got. Ideally, we'd have the following function overloads:
// Usual version, with runtime format string
template<typename... Args>
void printf(const char* fmt, const Args&... args)
{
// ...
}
// Version for compile time checking of format string (invalid C++)
template<int N, typename... Args>
void printf(constexpr char (&fmt)[N], const Args&... args)
{
static_assert(countFormatSpecs(fmt) == sizeof...(args));
// ...
}
However, there's no such thing as a constexpr
function argument, so the second function here can't be written in this form. In fact, I can't see any way to write it while preserving the usual syntax: printf()
itself is clearly not a constexpr function, so any compile time checking would presumably have to be done outside. However, it's only inside printf()
that we know the number of arguments passed. The only way I can see to combine these things is in the type signature, which at a quick glance is what the author of safe_printf
seems to have done. This is clever, but the unfortunately ugly and non-printf compatible syntax which results is a dealbreaker for tinyformat.
Having said that, it might still be possible to achieve a syntax like
printf(FMT("foo %d"), 100);
which could indeed be useful if you were using tinyformat via a logging macro. Of course, FMT
would have to be something longer in practice, at least by default.
In all this, it's worth keeping in mind that constexpr
functions have the interesting property that they must be callable at runtime as well as compile time. This means none of the usual compile time template tricks work on the arguments of constexpr function since those arguments themselves aren't constexpr variables.
from tinyformat.
Here's an amusing thought... we could likely implement this with nice syntax if we were willing to use macros π¬
For example
format_("foo %d", 100);
// expand to something like
format(check_format("foo %d", 100), 100);
wherecheck_format
returns "foo %d"
, or produces a compile time error if the arguments don't match.
from tinyformat.
Related Issues (20)
- Support for POSIX extension of positional arguments HOT 2
- const std::atomic actual parameter with requested alignment of 8 won't be aligned HOT 3
- Usage question: How to NOT display a parameter? HOT 1
- puts HOT 6
- Segmentation Fault with Intel Compiler HOT 10
- Cannot print volatile variables: error: invalid static_cast from type const volatile unsigned int* to type const void* HOT 1
- Why only 16 parameters instead of 32, 64, 128? HOT 7
- Use typename instead of class on TINYFORMAT_ARGTYPES? HOT 2
- Template program to count the number of format specifiers and check if they match the arguments count HOT 2
- Cannot print function pointer HOT 13
- Issues found during tinyformat audit (part of Bitcoin Core audit) HOT 5
- How to override the formatValue function?
- error: invalid static_cast from type βconst void*β to type βvoid (*)()β HOT 1
- Alter repository title or decouple from standard streams HOT 2
- Formatting NaN or infinity with explicit precision injects spurious zeros HOT 2
- Multiple symbol definitions after commit 9368985 HOT 2
- Passing -INF through %.16d still puts the minus sign at far-left HOT 1
- Different output for earlier compilers HOT 1
- Not enough conversion specifiers in format string HOT 4
- Update minimum CMake version
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 tinyformat.