Create va_list dynamically

后端 未结 7 729
一个人的身影
一个人的身影 2020-12-06 01:08

I have a function

void foo(int cnt, va_list ap);

I need to use it, but requirement is quite strict, number of va_list vary an

7条回答
  •  一整个雨季
    2020-12-06 01:43

    ...hmmm...maybe not portable...for sure not nice...but may solve yor problem...

    • va_list is (at least for visual c++) just a #define for char*
    • → arguments don't need to be on the stack
    • → arguments are just required to be continuous in memory
    • → no need to use assembler and/or copying (see my 'just for fun answer' :-)
    • → no need to worry about cleanup
    • efficient!
    • tested on w2k3 sp2 32bit + vc++ 2010
    
    #include 
    #include 
    #include 
    #include 
    
    #define N 6 // test argument count
    
    void foo(int n, va_list args);
    
    int main(int, char*[])
    {
        std::vector strings;
        std::wstring s(L"a");
        int i(0);
    
        // create unique strings...
        for (; i != N; ++i)
        {
            strings.push_back(s);
            ++s.front();
        }
        foo(N, reinterpret_cast(strings.data()));
        return 0;
    }
    
    void foo(int n, va_list args)
    {
        int i(0);
    
        for (; i != n; ++i)
            std::wcout << va_arg(args, std::wstring) << std::endl;
    }
    
    

提交回复
热议问题