Skip some arguments in a C++ function?

前端 未结 4 592
自闭症患者
自闭症患者 2020-12-01 21:37

I have a C++ function that has 5 arguments, all of which have default values. If I pass in the first three arguments, the program will assign a default value to the last t

4条回答
  •  时光说笑
    2020-12-01 21:54

    Not directly, but you might be able to do something with std::bind:

    int func(int arg1 = 0, int arg2 = 0, int arg3 = 0);
    
    // elsewhere...
    using std::bind;
    using std::placeholders::_1;
    auto f = bind(func, 0, _1, 0);
    
    int result = f(3); // Call func(0, 3, 0);
    

    The downside is of course that you are re-specifying the default parameters. I'm sure somebody else will come along with a more clever solution, but this could work if you're really desperate.

提交回复
热议问题