Results 1 to 6 of 6

Thread: va_start used in function with fixed args

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2006
    Posts
    27
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 2 Times in 1 Post

    Default Re: va_start used in function with fixed args

    Quote Originally Posted by jacek View Post
    Why do you need va_start in that function?
    I'm using void InternalLogFunction( char* Format, va_list& Args, const int, const bool ); so i need va_start to initialize the va_list.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: va_start used in function with fixed args

    Quote Originally Posted by stinos View Post
    I'm using void InternalLogFunction( char* Format, va_list& Args, const int, const bool ); so i need va_start to initialize the va_list.
    Indeed, I missed that.

    The problem is that the standard doesn't say anything about using va_list in a function with fixed number of parameters, so compilers might behave differently. The parameter you pass to va_start() is defined as the rightmost one and if you follow the standard strictly this makes va_start() useless in your case.

    The easiest solution would be to move those last two parameters to the front, but I assume you can't do that, so instead you could change your InternalLogFunction() to something similar to qDebug() stream:
    Qt Code:
    1. void Log( const char* Format, const T0 arg0, const int iLevel, const bool bLineEnd )
    2. {
    3. InternalLogStream( iLevel, bLineEnd, Format ) << arg0;
    4. }
    To copy to clipboard, switch view to plain text mode 
    or something like that.

  3. #3
    Join Date
    Sep 2006
    Posts
    27
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 2 Times in 1 Post

    Default Re: va_start used in function with fixed args

    Quote Originally Posted by jacek View Post
    The easiest solution would be to move those last two parameters to the front, but I assume you can't do that, so instead you could change your InternalLogFunction() to something similar to qDebug() stream:
    didn't think about that. Wouldn't it require partly reinventing the printf mechanism?

    Suppose a case with 3 parameters:
    Log( "%d %s %d", 3, "test", 3, level, true );
    that would use
    InternalLogStream( iLevel, bLineEnd, Format ) << arg0 << arg1 << arg2;
    Each call to << should move on to the next '%' found in the format string, convert the argument, append result to internal string. That's what printf does..


    edit after thinking it through: it's really not a bad idea at all. It would require a call to 'Flush' or so after the InternalLogStream() calls because it has no way of knowing which argument is the last one, but I like the fact it uses templates: there's no way you can write a call with an argument that doesn't support conversion as the compiler would spot it immedeately so it's safer than normal printf. It might be somewhat slower though, still have to figure that out.
    Last edited by stinos; 27th November 2008 at 08:13.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: va_start used in function with fixed args

    Quote Originally Posted by stinos View Post
    Wouldn't it require partly reinventing the printf mechanism?
    Yes, but you can't use printf() because of those last two parameters.

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 14:22
  2. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 12:57
  3. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 17:33
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.