I’m trying to learn more about compile-time constructs in C++, constexpr
, consteval
, etc. So I was trying to understand how the fmtlib does compile-time validation over the format parameters by using fmt::format_string
.
I’m now facing a problem that I just cannot figure out. This is where that structure is defined. For the sake of testing and learning, I’m trying to write my own super simplified version of this. I’ve started with the following:
template<class... Args>
struct MyFmtString {
template<class SV>
consteval MyFmtString(const SV&) {}
};
However, as soon as I try to create a function that takes this as an argument, I’m not able to compile it. For example:
template<class... Args>
void foo(MyFmtString<Args...> f, Args&&... args) {}
foo("test {}", 1);
That fails with:
...: note: template argument deduction/substitution failed:
...: note: mismatched types 'MyFmtString<Args ...>' and 'const char*'
Which I think makes sense, because as far as I know you cannot use implicit conversions with templates. I read about that in this question.
I’ve been trying to figure out how to write MyFmtString
so it will take an implicit const char* string like fmt does, but I can’t understand how this would ever work.
How is it that this works for fmt::format_string
? What am I missing or doing wrong?