[I can't figure out how to post this both here and comp.std.c++, so I
have to multi-post later. Unless a moderator here can do it.]
{ My experience suggests it's best to avoid cross-posting to moderated
groups -mod/we }
This is based on a recent Stack Overflow post at:
<
http://stackoverflow.com/q/16563797/1010226>. As a commenter to my
query indirectly mentioned, we can do this:
#include <cstdio>
#include <string>
template< typename T >
T const &printf_helper( T const &x )
{ return x; }
char const *printf_helper( std::string const &x )
{ return x.c_str(); }
template< typename ... Req, typename ... Given >
int wrap_printf( int (*fn)( Req... ... ), Given ... args )
{ return fn( printf_helper( args ) ... ); }
int main() {
wrap_printf( &std::printf, "Hello %s\n", std::string( "world!" ) );
wrap_printf( &std::fprintf, stderr, std::string( "Error %d" ), 5 );
}
channel C++ varargs into C varargs. But how do we go the other way?
How do we use the va_* stuff for C varargs when the next-to-last
argument is a C++ vararg? My limited testing concludes that it DOES
NOT work.
template < typename ...Args >
int my_func2( short x, std::va_list &aa, Args &&...a );
template < typename ...Args >
int my_func( short x, Args &&...a, ... )
{
std::va_list args2;
va_start( args2, a... );
//...
}
I used GCC 4.8 from a compiling website. I initially hoped that I
didn't need to use the "..." after the "a," but I had to. The pack
expansion reacted badly with the "va_start" macro. The code worked
correctly when "sizeof...(a)" was exactly one. It gave errors when
"a" was longer than one AND when it was zero-length. Also, when "a"
was zero-length, I could plug in "x" instead of "a..." and it worked.
What I expect to happen:
1. I can use "a" as the second parameter of "va_start" without the
"...". If that is not possible (since that usage is currently illegal
everywhere), then "va_start" has to work when "a..." is given. I
don't know how all this works, but maybe the C++ version of "va_start"
needs to be a variadic macro?
2. I cannot use "x" instead when "a..." is empty, since the two cases
can't co-exist because it has to be hard-coded. (Can the planned
"static_if" do it?) In the worse case scenario, the compiler will
secretly switch in "x" for "a..." if it detects that "a..." is empty;
the programmer doesn't have to type in anything different.
A fix:
Change the footnote (currently #227 in the C++11 standard) for Section
18.10 [support.runtime], paragraph 3 to:
227) Note that va_start is required to work as specified even if unary
operator& is overloaded for the type of parmN, or parmN is a pack
expansion, or both.
--
Daryle W.
[ See
http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]