fork download
  1. #include <cstdarg>
  2. #include <cstring>
  3. #include <cstdio>
  4.  
  5. /**
  6.   An example to demostrate an unexpected function overload...
  7.  
  8.   MyPrintf(fmt, char*) calls MyPrintf(fmt, va_list&)
  9.   instead of MyPrintf(fmt, ...)
  10.  
  11.   MyPrintf(fmt, const char*) calls as expected MyPrintf(fmt, ...)
  12.  
  13. */
  14.  
  15.  
  16. void MyPrintf(const char* fmt, ...)
  17. {
  18. printf("%s ('%s')\n", __PRETTY_FUNCTION__, fmt);
  19. }
  20.  
  21. void MyPrintf(const char* fmt, va_list& vargs)
  22. {
  23. printf("%s ('%s', vargs='%p')\n", __PRETTY_FUNCTION__, fmt, &vargs);
  24. }
  25.  
  26. int main()
  27. {
  28. char* str = strdup("hello all\n");
  29. printf("str@ %p\n", str);
  30.  
  31. MyPrintf("%s", (const char*) str); // calls MyPrintf(fmt, ...)
  32. MyPrintf("%s", str); // calls MyPrintf(fmt, va_list&)
  33.  
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
str@ 0x8618008
void MyPrintf(const char*, ...) ('%s')
void MyPrintf(const char*, char*&) ('%s', vargs='0xbfa62274')