How to wrap a function with variable length arguments?

后端 未结 7 2057
借酒劲吻你
借酒劲吻你 2020-12-05 12:54

I am looking to do this in C/C++.

I came across Variable Length Arguments but this suggests a solution with Python & C using libffi.

Now, if I want to wr

7条回答
  •  长情又很酷
    2020-12-05 13:34

    Are you using C or C++? The next C++ version, C++0x, will support variadic templates which provide a solution to that problem.

    Another workaround can be achieved by clever operator overloading to achieve a syntax like this:

    void f(varargs va) {
        BOOST_FOREACH(varargs::iterator i, va)
            cout << *i << " ";
    }
    
    f(args = 1, 2, 3, "Hello");
    

    In order to get this to work, the class varargs has to be implemented to override operator = that returns a proxy object which, in turn, overrides operator ,. However, making this variant type safe in current C++ isn't possible as far as I know since it would have to work by type erasure.

提交回复
热议问题