A mirroring question of Is it possible to pass va list to variadic template
In my project for testing, there is a function:
void Func(LPCSTR format, ...) {
va_list args;
va_start(args, format);
char buffer[256];
vsprintf_s(buffer, 256, format, args);
printf("%s\n", buffer);
va_end(args);
}
And I write a template function:
template<typename... Args>
void FuncTemp(LPCSTR format, Args... args) {
Func(format, args...); //#1
}
Is the call for Func
in line #1 right? I have tested my program, and it seemed to produce the correct results. Are there problems or pitfalls writing like this way?
The logic behind this: I want to realize a log writing class which can decide to write logs to local positions or submitte to servers:
Class LogWriting{
public:
...
LogTypes mLogType;
void WriteLogs(...){
switch (mlogType) {
case(LogTypes::local): {
// need to call <void LogLocal(LPCSTR format, ...)> here
// which CANNOT be changed.
break;
}
case(LogTypes::online): {
// need to call
/* template<typename... Args>
void LogOnline(LPCSTR format, Args... args)
{
std::string strFormat = std::string(format);
std::string logMessage = fmt::sprintf(strFormat, args...);
reportLog(logMessage);
}
*/
// which CANNOT be changed.
break;
}
...
}
};
Because I cannot change the parameters' types of LogLocal()
and LogOnline()
(one is va_list and another is variadic template), I decided to set WriteLogs()
as a variadic function template to suit these two functions:
template<typename... Args>
void WriteLogs(LPCSTR format, Args... args)