1

My employer company has an API package & I am asked to create a test for QA using a certain function of a class. The function is something like this:

void func(int a, int b, int c, int d, int e);

where b & d are default parameters. The types are obviously not int, I have created them here for understanding purpose. Currently I can use that function by passing NULL for the default args. But I was thinking in the case of above function func, if the default value for b & d was non-zero say 1 & the user did not know this & if he used NULL (which is = 0) then a different result would be arrived.

So I would like to know how should I create such functions that have default args between non-default ones & I should not be needed to pass anything for that arg i.e.

func(23, , 34, , 45);

Currently VS2010 is giving me compile error for similar implementation for our company's API call.

I hope I made the question pretty clear to understand.

I tried searching similar question elsewhere too but could not find solution for my query.

Thank You.

5
  • 2
    Having default parameters followed by non-default parameters isn't valid C++. If this works on some other compiler you have, you are probably using a non-standard extension that VS2010 doesn't support. Commented Mar 6, 2013 at 10:38
  • @MarceloCantos sorry but I never told that such code was compiling! I was asking for a workaround! Commented Mar 6, 2013 at 10:44
  • It was always a mystery for me why this is not allowed func(23, , 34, , 45); two successive commas would mean use the default param here Commented Mar 6, 2013 at 10:45
  • 1
    The best thing to do in that situation is to think long and hard about what parameters will always be needed, then determine which one will be the next most included variable, so on and so forth and put them in that order so that the most typical cases are handled first cascading downward. Commented Mar 6, 2013 at 10:49
  • +1 Being appreciative goes a long way! Commented Mar 6, 2013 at 11:02

5 Answers 5

5

You can't do that, you have to put all non-optional parameters first, followed by optional parameters after.

You can read a bit more about optional parameters here. Microsoft says:

Default arguments are used only in function calls where trailing arguments are omitted — they must be the last argument(s). Therefore, the following code is illegal:

int print( double dvalue = 0.0, int prec );
Sign up to request clarification or add additional context in comments.

Comments

4
void func(int a, int c, int e)
{
    int default_b = 1;
    int default_d = 1;
    func(a, default_b, c, default_d, e);
}

Otherwise you simply can't, as there is no named parameters in C++.

(Though another solution would be to use boost::bind or a similar functor).

If you can modify the original function, you can simply use default value at the condition the defaulted parameter are at the end :

void func(int a, int c, int e, int b = 1, int d = 1);

4 Comments

I think your answer in incorrect because then how will i be able to pass non-default values for b & d through func()?
Like using func(a,b,c,d,e) ? It's a simple function overload, so you can still use the other declaration :) (and my implementation use it btw)
That's a slick solution. But isn't it one to a bad design. Wouldn't it make more sense to make the default parameters the last two?
@C.Lang it would make more sense, but when you can't change the original function you have to find a workaround. Typically this API may be written in C or simply can't be modified by OP.
2

How do you expect the compiler to know that you mean to pass a,c and e and not some other triplet? In a function declaration all parameters after a parameter with default value should have default values. So you can never have default b and d - whenever you pass only three parameters to this function it would mean you want to use the default values for the last 2 parameters and these are d and e. Move your optional parameters as last in the function.

3 Comments

Thank You for the answer but our company currently ships APIs which such functions, but their parameters are not simple int, hence passing NULL solves the problem. So do you mean I can tell my boss that those funcs are incorrect C++ usage?
@CAD_coding what you say is completely different. Passing NULL as argument is not at all as ommitting the argument.
the previous tests that I have seen have used NULL as arg, but thats internal within our company. The product doc says that these are default args, so I dont know how our customers are using it. But isnt it a bad technique?
0

There is no direct way to achieve what you are looking for.

The first alternative would be the use of boost::optional<T> for the parameters.

Another alternative might be named parameters, as implemented in the Boost Parameter Library.

If you think that this might be something for you and you want further elaboration on how these could be used for your use case, let me know and I'll edit this answer.

1 Comment

I think @Geoffroy's solution is brilliant! Thank You for your answer also.
0

There is a boost library for this boost::parameter http://www.boost.org/doc/libs/1_53_0/libs/parameter/doc/html/index.html

It supports named parameters in arbitrary order with default values for non used parameters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.