non-POD type in call to va_start

R

Ross

Hi,

I have the following program:

#include <stdarg.h>

class A {
public:
A() { }
}

void f(A a,...) {
va_list l;
va_start(l,a);
// ...
va_end(l);
}

When I try to compile this program (using gcc), the compiler moans
about the non-POD type in the call to va_start() and threatens to abort
at runtime. Having checked the C-spec, this seems reasonable. What I'm
looking for is some kind of work-around. Is there any way I can get
this work (preferably without changing f() to take an A* argument)? Any
suggestions would be greatly appreciated (even using macroes).

Thanks in advance,
Ross
 
R

Rolf Magnus

Ross said:
Hi,

I have the following program:

#include <stdarg.h>

class A {
public:
A() { }
}

void f(A a,...) {
va_list l;
va_start(l,a);
// ...
va_end(l);
}

When I try to compile this program (using gcc), the compiler moans
about the non-POD type in the call to va_start() and threatens to abort
at runtime. Having checked the C-spec, this seems reasonable.

It does? There are no other types than POD in C. Or did you mean the C++
spec?
What I'm looking for is some kind of work-around. Is there any way I can
get this work (preferably without changing f() to take an A* argument)?
Any suggestions would be greatly appreciated (even using macroes).

Well, variable argument lists cannot be used with non-POD types. I don't
think there is a workaround to get it do what you want.
 
A

amirkam1

void f(A a, int nDummy,...) {

You can use a dummy integer as the identifier of the rightmost named
parameter. Pass this nDummy to the va_start macro. I think this should
work. (Also the non-PODs should not be passed as a variable argument to
this function.)

Amir Kamerkar
 
A

Andrey Tarasevich

Rolf said:
Well, variable argument lists cannot be used with non-POD types. I don't
think there is a workaround to get it do what you want.

Well, the "non-POD" restriction does indeed apply to the arguments that
are passed as _variable_ ones (i.e. correspond to the '...' portion of
parameter declaration list), but I don't see anything in the standard
that would also extend this requirement to the explicitly declared
parameters, including the one used in 'va_start'. Am I missing something?
 
R

Ross

Andrey said:
Well, the "non-POD" restriction does indeed apply to the arguments that
are passed as _variable_ ones (i.e. correspond to the '...' portion of
parameter declaration list), but I don't see anything in the standard
that would also extend this requirement to the explicitly declared
parameters, including the one used in 'va_start'. Am I missing something?

As I understood it, the argument passed to va_start() should have the
type that results if you were to pass an argument of that type to an
ellipsis. Therefore, it can't be a non-POD type because it's undefined
to pass a non-POD type to an ellipsis. I don't have my copy of the spec
to hand, though, so I could be wrong.

Ross
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top