Optional parameters

As a matter of convenience, I would like to implement optional parameters into my functions to maintain a legacy interface for a set of users. For example, a single function could be overloaded to handle:

1
2
3
function(arg1, arg2)
function(arg1)
function(,arg2)


Where by any missing argument would maintain its previous value. The last one is of concern here because taking a single argument must be correctly understood.

Any ideas?
You can only leave out parameters to the right so the third function call will not work. If arg1 and arg2 is of different types you could make a function that takes a single argument of the type of arg2, and call it as function(arg2). That is probably only a good idea if the two types are not implicit convertible to each other or it could be confusing.
Yes, I know all of this but am asking for a possible out-side-of-the-box solution if one even existed.

They are of the same type btw. I had previously sent NULL as an argument that the function would check for and disregard appropriately. But that was messy.

Thanks for the reply.
If creating your own types that are not implicitly convertible is an option you could do that, then you could have single argument functions for the different types where needed (when a default param is not used).
Sorry. What you are looking for is not supported in C++. Optional parameters must be omitted starting at the end. In the following function:

void foo(int a = 1, string b = "hi", float c = 5.3);

valid calls can only be of the form:

1
2
3
4
foo(10, "coding", 3.5");
foo(10, "coding");
foo(10);
foo(); 


You are not allowed to use optional values that appear before values you will provide (unless, of course, that changed in C++0x11).
Last edited on
No possible way to do it even with witch-craft. As I thought. Thanks.
Hmm. I'm not sure I understood the problem here >.>
What doug4 said may be true, but are you actually trying to make a function that has optional parameters for parameters after the first one? Or are you trying to do something else.. I wants to know O.o
You could play around with the named parameter idiom.

Alas, crying baby. Gotta go.
So the user has previously used a program that allowed for limited scripiting/coding. I have no idea what it was originally coded in. It allowed for something like:

statement arg1, arg2

It wasn't a classical function, ie it didn't return anything. But it supported:

statement , arg2

Where the behavior would keep arg1 as its previous value and only make the necessary changes with arg2. These 'translated' functions in my port behave the same way, but you are forced to supply all arguments leaving a lot of redundancy.

I always thought it would be a nice feature of some languages to support functions in this way where an argument was left null without actually explicitly submitting NULL. Previously I did this very thing, but it looks like garbage:

function(NULL, 45, "test", NULL, NULL, 8);

could be:

function(, 45, "test", , , 8);

Again, convenience on the client side. I was intrigued by the request.
Last edited on
Oh I see. Thanks. Sounds like something -almost- useful.
Why don't you just make one function that takes in an array?
Or, make a function that takes in an unspecified amount of arguments, and then figures out what they apply to.

I think you can mock this named parameter idiom with some complicated code.
Like a multi-dimensional one? I'm trying to conceptualize how that could work... A function with arbitrary arguments? If many arguments are of the same type, how could it make heads or tails?

You could play around with the named parameter idiom.
I think you can mock this named parameter idiom with some complicated code.

I'm honestly not familiar with this. Got any good info/explanations?

Maybe what I am asking for could only be done with a custom interpreter, or by feeding a text file to a Lexical Analyzer (http://www.cplusplus.com/forum/general/69195/ )! Ha!

Last edited on
Um.. Well. Think about it logically.

What exactly will you be feeding into the function?
How does the function know what ( , arg2) arg2 is? Cus its in the second spot, right?

Then what is arg2? What is arg1? What is arg_n? Maybe the function knows. Maybe you know. But does the user know? Does he know enough about the function to know that he wants to change arg2 instead of arg20? If he does, why not just make a class? Then he doesn't have to worry about which spot he needs to put his argument. He can easily remember the name of what he wants to change.

If this function with named parameters has more than 20 parameters, then how would the user know which spot hes at without counting the commas? What if he counts wrong? Sounds like a pile of error. In your defense, you could say, "well obviously by the name of the arg.." In my defense, make a class. Takes a load off your chest.

I would try to avoid creating named parameters.
Well then I follow. It's already in an OO paradigm and each function is overloaded to the brim. You can throw anything at it already, except empty arguments.

But, again, it is a language construct thing. I just got back to the C/C++ scene so I am forgiving.
Hmm.. So it's actually a feature in a different language? That is interesting. I cannot see how it would help anyone. But I can see that it is interesting.
As Duoas pointed out:
You could play around with the named parameter idiom.


You could do something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class ParameterWrap
{
    public:
    ParameterWrap& arg1(const arg1_type &arg)
    {
          m_arg1 = arg; 
    }
     ParameterWrap& arg2(const arg2_type &arg);
     ParameterWrap& arg3(const arg3_type &arg);
     //... argN

private:
     m_arg1;
     //... m_argN;  //Maybe use a container, I don't know this is just a simple example...
}

//.. Then in use..

function(wrapper.arg1(4).arg7("XLE").arg2(-30).arg13(0.103));  //Using the named parameters, could use all, could use none... 


http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.20
Last edited on
I got an idea! LOL. Make the function accept a string.
Then make it delimit the string by commas. Lol. It's cheezy, but it just might work.
Last edited on
Reminds me of a language (can't remember which one) where it didn't support the capability to make structure arrays/user-defined type arrays. My solution was to make every component in the struct a string, and used it like an array.

What a mess.

@clanmjc, thanks! It doesn't look foreign, but I see some thing in there that I didn't know you could do.
Yeah. By using references, you can continuously go through and add args on. Same thing you do with streams.

myStream << agr1 << arg2 << arg3 << arg4.

It looks nice.
Notice the order of the arguments as well as them even being existent, don't matter.
Topic archived. No new replies allowed.