c++ - Log(PCTSTR format,...) and Log(PCTSTR text): error C2668 ambiguous call to overloaded function -
i have following defined:
void logmessage(pctstr text); void logmessage(pctstr format, ...);
if want call function 1 parameter, following error message:
source.cpp(10): error c2668: 'log' : ambiguous call overloaded function 'void log(pctstr,...)' or 'void log(pctstr)' while trying match argument list '(const wchar_t [42])'
is possible static_cast explizite use first version? or haw can solved, except renaming first or second function?
how following? haven't tested on vc++ (which seems platform of choice) version using implements enough c++11 work.
#include <iostream> #include <cstdio> #include <cstdarg> void logmessageworker(char const* format, ...) { // 1k should enough anyone... ;) char buf[1024] = { 0 }; // version of vsnprint called should null terminate correctly , doesn't // strictly need -1 believe implementation included // vc++ leaves lot desired may need tweak this. va_list args; va_start (args, format); vsnprintf (buf, sizeof (buf) - 1, format, args); va_end (args); std::cout << "logmessage: " << buf << std::endl; } template <class... arguments> void logmessage(char const* format, arguments... arguments) { logmessageworker (format, std::forward<arguments>(arguments)...); } void logmessage(char const* text) { logmessageworker ("%s", text); } int main(int argc, char **argv) { logmessage ("the test starting..."); (int = 0; < 3; i++) logmessage ("this test #%d", i); logmessage ("this contains % character , still works (%d-%d-%d-%d)"); return 0; }
Comments
Post a Comment