Passing std::va_list by reference to const.

V

Vaclav Haisman

Hi,
I have the following code. I wonder if it is valid to pass std::va_list
around by reference to const like this:

//-----8<--------
#include <cstdarg>
#include <cstdio>


void foo (char const * fmt, std::va_list const & l)
{
std::vprintf(fmt, l);
}

void bar (char const * fmt, ...)
{
std::va_list l;
va_start (l, fmt);
foo (fmt, l);
va_end (l);
}

int main ()
{
bar ("%s", "test\n");
}
//-----8<--------

This code produces the following error with GCC on FreeBSD/AMD64, but
compiles and works well on 32bit host:

va_test.cxx: In function 'void foo(const char*, const __va_list_tag (&)[1])':
va_test.cxx:7: error: invalid conversion from 'const __va_list_tag*' to
'__va_list_tag*'
va_test.cxx:7: error: initializing argument 2 of 'int vprintf(const char*,
__va_list_tag*)'
 
A

Alf P. Steinbach

Hi,
I have the following code. I wonder if it is valid to pass std::va_list
around by reference to const like this:

//-----8<--------
#include<cstdarg>
#include<cstdio>


void foo (char const * fmt, std::va_list const& l)
{
std::vprintf(fmt, l);
}

void bar (char const * fmt, ...)
{
std::va_list l;
va_start (l, fmt);
foo (fmt, l);
va_end (l);
}

int main ()
{
bar ("%s", "test\n");
}
//-----8<--------

This code produces the following error with GCC on FreeBSD/AMD64, but
compiles and works well on 32bit host:

va_test.cxx: In function 'void foo(const char*, const __va_list_tag (&)[1])':
va_test.cxx:7: error: invalid conversion from 'const __va_list_tag*' to
'__va_list_tag*'
va_test.cxx:7: error: initializing argument 2 of 'int vprintf(const char*,
__va_list_tag*)'

The following code reproduces your error message:


<code>
struct VaListTag {};
typedef VaListTag VaList[1];

void vPrintf( char const fmt[], VaList ) {}

void foo (char const * fmt, VaList const& arglist )
{
vPrintf( fmt, arglist );
}

int main ()
{
VaList argList = {};
foo( "%s", argList );
}
</code>


A practical solution is to pass by value.

I can't find any place where the C++ the standard says anything about what kind
of type a std::va_list can be.


Cheers & hth.,

- Alf
 
V

Vaclav Haisman

Pete Becker wrote, On 15.5.2010 16:57:
va_list is magic. Don't do anything fancy with it. Pass it by value.
I am doing that. But why is it so magical? Shouldn't it be a regular object?
And should not the magic be contained inside the regular object?
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top