From: DeMarcus on
Hi,

I need to provide a set of parameters to a function where one or more
can be optional. Standard C++ can only provide default values for the
last parameters and a default value representing N/A for references is
not quite pleasant.

I came up with an idea of overloading the optional parameters. Please
give me your feedback.

class SomeClass
{
public:

void fnc( int& a );
void fnc( int& a, int& b );
void fnc( int& a, int& b, int& c );

// Now insert a pointer if we want to disable (b).
void fnc( int& a, int* b, int& c );
};

int main()
{
SomeClass sc;
int a, b, c;

// Just want to provide (a).
sc.fnc( a );

// Just want to provide (a) and (b).
sc.fnc( a, b );

// Just want to provide (a) and (c) but not (b).
// Tricky to get right normally, but with this overloading
// it works!
sc.fnc( a, NULL, c );

}

Is this way to provide optional parameters ok, or will I bump into
trouble later I haven't foreseen here?

My next question is; is there a way to use some technique like traits or
similar to make sure at compile time that (b) for sure is NULL?


Thanks,
Daniel

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 30 Mrz., 17:49, DeMarcus <use_my_alias_h...(a)hotmail.com> wrote:
> I need to provide a set of parameters to a function where one or more
> can be optional. Standard C++ can only provide default values for the
> last parameters and a default value representing N/A for references is
> not quite pleasant.
>
> I came up with an idea of overloading the optional parameters. Please
> give me your feedback.
>
> class SomeClass
> {
> public:
>
> void fnc( int& a );
> void fnc( int& a, int& b );
> void fnc( int& a, int& b, int& c );
>
> // Now insert a pointer if we want to disable (b).
> void fnc( int& a, int* b, int& c );
> };

Do you mean "Now insert a **NULL** pointer if we want to disable"?
If not this interface would be quite misleading. Why should a pointer
to a valid int not be accepted?

> int main()
> {
> SomeClass sc;
> int a, b, c;
>
> // Just want to provide (a).
> sc.fnc( a );
>
> // Just want to provide (a) and (b).
> sc.fnc( a, b );
>
> // Just want to provide (a) and (c) but not (b).
> // Tricky to get right normally, but with this overloading
> // it works!
> sc.fnc( a, NULL, c );
> }
>
> Is this way to provide optional parameters ok, or will I bump into
> trouble later I haven't foreseen here?

I cannot answer this question, because the answer depends on
the evolution of your parameters and the question, which
parameter combinations make sense.

> My next question is; is there a way to use some technique like traits or
> similar to make sure at compile time that (b) for sure is NULL?

Maybe a recent article of Alf

http://groups.google.de/group/comp.lang.c++.moderated/msg/178d5c2061d010f8

gives you an alternative approach?

HTH & Greetings from Bremen,

Daniel Kr�gler


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: DeMarcus on
Daniel Kr�gler wrote:
> On 30 Mrz., 17:49, DeMarcus <use_my_alias_h...(a)hotmail.com> wrote:
>> I need to provide a set of parameters to a function where one or more
>> can be optional. Standard C++ can only provide default values for the
>> last parameters and a default value representing N/A for references is
>> not quite pleasant.
>>
>> I came up with an idea of overloading the optional parameters. Please
>> give me your feedback.
>>
>> class SomeClass
>> {
>> public:
>>
>> void fnc( int& a );
>> void fnc( int& a, int& b );
>> void fnc( int& a, int& b, int& c );
>>
>> // Now insert a pointer if we want to disable (b).
>> void fnc( int& a, int* b, int& c );
>> };
> Do you mean "Now insert a **NULL** pointer if we want to disable"?
> If not this interface would be quite misleading. Why should a pointer
> to a valid int not be accepted?

Yes, you are right. Sorry that I forgot to emphasize that. Of course it should be a NULL pointer, and preferably checked for NULL at compile time if that's possible with some template magic.

>> int main()
>> {
>> SomeClass sc;
>> int a, b, c;
>>
>> // Just want to provide (a).
>> sc.fnc( a );
>>
>> // Just want to provide (a) and (b).
>> sc.fnc( a, b );
>>
>> // Just want to provide (a) and (c) but not (b).
>> // Tricky to get right normally, but with this overloading
>> // it works!
>> sc.fnc( a, NULL, c );
>> }
>>
>> Is this way to provide optional parameters ok, or will I bump into
>> trouble later I haven't foreseen here?
> I cannot answer this question, because the answer depends on
> the evolution of your parameters and the question, which
> parameter combinations make sense.
>> My next question is; is there a way to use some technique like traits or
>> similar to make sure at compile time that (b) for sure is NULL?
> Maybe a recent article of Alf
> http://groups.google.de/group/comp.lang.c++.moderated/msg/178d5c2061d010f8
> gives you an alternative approach?

I saw that post, and actually in my current situation that will probably be a clean solution. However, I still want to elaborate with the solution here for future tasks.


> HTH & Greetings from Bremen,
> Daniel Kr�gler

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel T. on
In article <4bb1fa6e$0$279$14726298(a)news.sunsite.dk>,
DeMarcus <use_my_alias_here(a)hotmail.com> wrote:

> Hi,
>
> I need to provide a set of parameters to a function where one or more
> can be optional. Standard C++ can only provide default values for the
> last parameters and a default value representing N/A for references is
> not quite pleasant.
>
> I came up with an idea of overloading the optional parameters. Please
> give me your feedback.
>
> class SomeClass
> {
> public:
>
> void fnc( int& a );
> void fnc( int& a, int& b );
> void fnc( int& a, int& b, int& c );
>
> // Now insert a pointer if we want to disable (b).
> void fnc( int& a, int* b, int& c );
> };
>
> int main()
> {
> SomeClass sc;
> int a, b, c;
>
> // Just want to provide (a).
> sc.fnc( a );
>
> // Just want to provide (a) and (b).
> sc.fnc( a, b );
>
> // Just want to provide (a) and (c) but not (b).
> // Tricky to get right normally, but with this overloading
> // it works!
> sc.fnc( a, NULL, c );
>
> }
>
> Is this way to provide optional parameters ok, or will I bump into
> trouble later I haven't foreseen here?

Don't beat yourself up like this, just do:

class SomeClass {
public:
void func_a(int a);
void func_ab(int a, int b);
void func_ac(int a, int c);
void func_abc(int a, int b, int c);
};

It will be a lot clearer at the calling site what is going on too.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Seungbeom Kim on
On 2010-03-30 08:49, DeMarcus wrote:
> Hi,
>
> I need to provide a set of parameters to a function where one or more
> can be optional. Standard C++ can only provide default values for the
> last parameters and a default value representing N/A for references is
> not quite pleasant.
>
> I came up with an idea of overloading the optional parameters. Please
> give me your feedback.
>
> class SomeClass
> {
> public:
>
> void fnc( int& a );
> void fnc( int& a, int& b );
> void fnc( int& a, int& b, int& c );
>
> // Now insert a pointer if we want to disable (b).
> void fnc( int& a, int* b, int& c );
> };

Doesn't the last overload have to deal with non-null values of b anyway?
Then I don't see the value of having the third overload separately.

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]