English 中文(简体)
C++功能的所有论点
原标题:looping over all arguments of a function in C++
  • 时间:2011-11-04 12:56:09
  •  标签:
  • c++

我想对一项职能的争论进行同样的处理。 是否有办法推翻所有论点? 我在这样做时遵循了守则,但希望看到这样做有条不紊。

 void methodA(int a1, int a2, int b1, double b2){   
        //.. some code 
        methodB(a1, f(a1));
        methodB(a2, f(a2));
        methodB(b1, f(b1));
        methodB(b2, f(b2));
        // more code follows ...

   }

int f(int a){
      // some function. 
   return a*10;
}

double  f(double b){
   return b/2.0;
}
最佳回答

You could use variadic templates:

template <typename T, typename ...Args>
void methodAHelper(T && t, Args &&... args)
{
  methodB(t, f(t));
  methodAHelper(std::forward<Args>(args)...);
}

void methodAHelper() { }

template <typename ...Args>
void methodA(Args &&... args)
{
  // some code
  methodAHelper(std::forward<Args>(args)...);
  // some other code
}

You can possibly get rid of the && and the forwarding if you know that your methodB call doesn t know about rvalue references, that would make the code a bit simpler (you d have const Args &... instead), for example:

methodAHelper(const T & t, const Args &... args)
{
  methodB(t, f(t));
  methodAHelper(args...);
}

You might also consider changing methodB: Since the second argument is a function of the first argument, you might be able to only pass the first argument and perform the call to f() inside the methodB(). That reduces coupling and interdependence; for example, the entire declaration of f would only need to be known to the implementation of methodB. But that s depends on your actual situation.

或者,如果只有one超载methodB 第一个论点是<代码>T,然后由您通过<代码>std:vector<T> to methodA,并随附:

void methodA(const std::vector<T> & v)
{
  // some code
  for (auto it = v.cbegin(), end = v.cend(); it != end; ++it)
    methodB(*it, f(*it));
  // some more code
}

int main() { methodA(std::vector<int>{1,2,3,4}); }
问题回答

是的,你回顾的概念称为variadic=

取决于你试图做什么。 最简单的事可能是重新审视你的职能,看看看它是否能够采取阵列或 st:作为论据。 单程航道的通向更简单





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签