Click here to Skip to main content
Click here to Skip to main content

va_list, va_start, va_pass!!! or how to pass variable-argument list to next va-function

, 29 Mar 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
trick to pass variable-argument list to next va-function


I've been looking for solution of passing variable-argument list from my va-function to another one, like TRACE for example. All solutions I saw were about using special functions that take va_list as argument. But this is a un-straight way. Why couldn't I just pass "..." to next function? C++ syntax doesn't allow this. But C++ allows to extend itself. Let me introduce you new macros from va_ set:

template<byte count>
struct SVaPassNext{
    SVaPassNext<count-1> big;
    DWORD dw;
template<> struct SVaPassNext<0>{};
//SVaPassNext - is generator of structure of any size at compile time.

class CVaPassNext{
    SVaPassNext<50> svapassnext;
    CVaPassNext(va_list & args){
		try{//to avoid access violation
			memcpy(&svapassnext, args, sizeof(svapassnext));
		} catch (...) {}
#define va_pass(valist) CVaPassNext(valist).svapassnext

#if 0 //using:
void MyVA_Function(szFormat, ...){
    va_list args;
    va_start(args, szFormat);
    SomeOtherVA_Function(szFormat, va_pass(args));
how this works:
I just copy 50 * sizeof(DWORD) bytes of stack to my struct of this size and simply pass this struct as ... argument to next function. Compiler just copies my copy of local stack to next function stack. And that's all we need.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Russian Federation Russian Federation
Real christian since 1997 (to be a cristian means to follow Jesus Christ)
Programmer since 1999
main language is C++ and I fond of it!
Married to beautiful and clever sister in Christ since 2001
Musician lead guitar - since 1995, vocal since 2004. I play neoclassic-doom-progressive-death metall.

Comments and Discussions

QuestionGood! PinmemberWuRunZhe5-Dec-13 21:58 
SuggestionPortable or not, variadic functions themselves are evil PinmemberPa3PyX14-Dec-11 13:25 
GeneralMy vote of 2 PinmemberMichael Imamura6-Apr-09 19:41 
General[Message Deleted] PinmemberIlya Parniuk19-Feb-08 3:04 
GeneralRe: Crash on memcpy Pinmemberaraud19-Feb-08 3:14 
Generalnot portable Pinmemberpeterchen29-Mar-05 7:20 
GeneralRe: not portable Pinmemberaraud29-Mar-05 19:55 
GeneralRe: not portable Pinmemberpeterchen29-Mar-05 20:18 
Generalportable Pinmemberaraud29-Mar-05 22:41 
GeneralRe: portable PinsussAnonymous22-Jun-05 3:19 
GeneralRe: portable PinmemberJan Richardson7-Aug-05 2:48 
GeneralRe: portable Pinmember__PPS__18-Apr-07 15:37 
GeneralRe: portable Pinmemberaraud18-Apr-07 19:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150129.1 | Last Updated 29 Mar 2005
Article Copyright 2005 by araud
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid