Passing Variable Number of Arguments with different type - C++

后端 未结 6 826
闹比i
闹比i 2020-11-30 09:33

I am coding in C++ and have a few questions regarding the ellipsis:

  1. Is it possible to pass in class or class pointer into the ellipsis?

  2. Basi

6条回答
  •  攒了一身酷
    2020-11-30 09:49

    You can pass whatever you want to variadic functions, but this won't help you in writing convenient functions as you are losing the type information for the arguments.

    Depending on what you want to achieve there are better alternatives:

    • chaining operators like << or ():

      helper() << a << b << c;
      helper(a)(b)(c);
      
    • using (pseudo-)variadic templates:

      template           void func(T0 t0)        { ... }
      template void func(T0 t0, T1 t1) { ... }
      // ...
      
      // or if you can use C++0x features:
      template void func(Args... args) { ... }
      
    • ... more?

提交回复
热议问题