gcc va_arg does not support non-pod types

This is a discussion on gcc va_arg does not support non-pod types within the C++ Programming forums, part of the General Programming Boards category; I have a function that uses the va_list and its associated functionality, and it works great on vc++ with non-pod ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,590

    gcc va_arg does not support non-pod types

    I have a function that uses the va_list and its associated functionality, and it works great on vc++ with non-pod types, but gcc gives me warnings about it. is there any way, short of hacking the standard library, to make it accept std::string and other aggregate types?

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by Elkvis View Post
    I have a function that uses the va_list and its associated functionality, and it works great on vc++ with non-pod types, but gcc gives me warnings about it. is there any way, short of hacking the standard library, to make it accept std::string and other aggregate types?
    For portability's sake, don't try to use varargs with non-POD types. You'll have to alter your function to take a pointer to the object instead of the object itself. Working with pointers is no problem.

    EDIT: And in C++ there are usually preferable alternative to varargs in the first place. More detail about what you're doing might lead to a better solution.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Why would you want to do that? Passing non-PODs to varargs is undefined, full stop. VC++ would be a better compiler if it disallowed it too. I've got some code to maintain that liberally uses CString::Format and passes CString arguments cast to const char* as format arguments. It's a real hassle to make sure that every CString is cast correctly, since VC++ neither rejects the CString, nor is it capable of parsing and validating the format string as GCC does.

    Also, hacking the standard library wouldn't help. You'd have to hack the compiler. But if it's only a warning, I suppose you can just ignore it, at your own peril.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by CornedBee View Post
    Also, hacking the standard library wouldn't help. You'd have to hack the compiler. But if it's only a warning, I suppose you can just ignore it, at your own peril.
    Yeah -- I would expect that va_arg does a straight memcpy() from the stack into the provided space, breaking object copy semantics.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 02:29 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 06:20 PM
  5. gcc 3.1
    By itld in forum C++ Programming
    Replies: 1
    Last Post: 05-18-2002, 05:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21