I have a simplified version of a class that I named Printer
for SO. The real version takes data of various types and writes them to a buffer. I also have a method that can give the compile time length of a CString called constexprStrlen
:
#include <cstring>
#include <iostream>
inline constexpr int constexprStrlen(const char* const a_cstr) {
int i = 0;
while (a_cstr[i] != 0)
++i;
return i;
}
class Printer final {
void appendAll() {}
public:
void append(const char* const a_cstr) {
std::cout << a_cstr << " (length unknown)" << std::endl;
if (!a_cstr)
return;
write(a_cstr, ::strlen(a_cstr));
}
void append(const char* const a_cstr, const int a_len) {
std::cout << a_cstr << " " << a_len << std::endl;
if (!a_cstr)
return;
write(a_cstr, a_len);
}
// These are not the actual signatures I use, all that matters for SO is
// that there are other append methods besides the two above
void append(bool ) {} // Content not relevant for SO
void append(int ) {} // Content not relevant for SO
void append(long long) {} // Content not relevant for SO
void append(double ) {} // Content not relevant for SO
template <class TArg, class... TArgs>
void appendAll(TArg&& a_arg, TArgs&&... a_args) {
append(std::forward<TArg>(a_arg));
appendAll(std::forward<TArgs>(a_args)...);
}
void write(const char* const a_cstr, const int a_len) {
// Content not relevant for SO
}
};
int main() {
const char* const cstr1 = "abc";
const char* const cstr2 = "def";
Printer p;
p.append(cstr1); // Length unknown
p.append(cstr2, constexprStrlen(cstr2)); // Length compile time known
p.appendAll(cstr1, cstr2); // Both length unknown
return 0;
}
I can avoid using strlen
by calling append(const char*, int)
and passing the result of constexprStrlen
. My question is if there is any way to introduce constexprStrlen
into appendAll
so that it uses that instead of strlen