Computer Science Canada

Something important C++ is missing

Author:  wtd [ Sun Jul 16, 2006 10:12 am ]
Post subject:  Something important C++ is missing

Yet another language comparison. I'm going to look at something you should realize and appreciate that C++ lacks; something it very much should have.

In C++ we can create functions which have parameters with default values. However, if we wish to do something different in the body of the function, depending on whether or not the function was called with the argument provided, we must avoid using that feature entirely and instead use function overloading.

We cannot rely on testing to see if the provided argument equals the default value for that parameter. The programmer who used the function might call it with that value explicitly.

Ironically it's an older language that provides this greater functionality. Let's compare some Common Lisp and C++.

code:
void foo()
{
   cout << "I'll have to assume: \"bar\"" << endl;
}

void foo(string bar)
{
   cout << bar << endl;
}


code:
(defun foo (&optional (bar "bar" bar-p))
   (if bar-p
       (format t "I'll have to assume \"~A\"~%" bar)
       (format t "~A~%" bar)))

Author:  Null [ Sun Jan 14, 2007 9:15 pm ]
Post subject:  RE:Something important C++ is missing

wtd, please don't misunderstand the following. I admire you for spending so much time helping newbies and generally being a great, knowledgeable person on these forums.

I'm just fed up with these "Why this language is better then that one" topics.
It's getting old, and I don't think this belongs in the C++ tutorials section.

No offense intended. Smile

Author:  Clayton [ Sun Jan 14, 2007 10:08 pm ]
Post subject:  Re: Something important C++ is missing

If you can't stand them, then don't read them. I find these quite useful. Even if I've never coded anything in the languages, I always find out something new. Because not all languages are strong in the same areas, there are languages out there better for some things than other languages. If you've got a problem with it, then I guess you're sol.

Author:  wtd [ Sun Jan 14, 2007 10:24 pm ]
Post subject:  RE:Something important C++ is missing

This isn't a "language X is better than language Y" issue. Comparing and contrasting languages provides a greater understanding of all languages involved, and a greater understanding of a language makes it possible to write better code.

Author:  r.3volved [ Sun Jan 14, 2007 10:53 pm ]
Post subject:  RE:Something important C++ is missing

Can you elaborate on the point you're trying to get across?
I don't know lisp and I'm not sure what you're trying to show here...

What is that lisp doing line by line?

Author:  wtd [ Sun Jan 14, 2007 11:59 pm ]
Post subject:  RE:Something important C++ is missing

In C++, it's possible to provide default values for arguments. However, there's no way to discern whether or not the user actually supplied a value when they called it, or if they relied on the default value. This cannot be done, even with sentinel values.

The result is that in this eventuality, we fall back on overloading the method.

In the Common Lisp code, we say that the function foo has a parameter bar. That parameter has a default value of "bar". However, we also have bar-p, which is a boolean. If the user calls:

code:
(foo)


Then bar-p is false.

If we call:

code:
(foo some-value)


Then bar-p is true.

Author:  Clayton [ Mon Jan 15, 2007 12:04 am ]
Post subject:  Re: Something important C++ is missing

so then how does the program discern which output to print?

Author:  Craige [ Mon Jan 15, 2007 11:10 am ]
Post subject:  RE:Something important C++ is missing

Curiously, why would you care if the user offered a value, or fell back on a default? As long as the value is there, it should not reflect a problem in your code.

Maybe it's just that I've never come across such a situation though.

Author:  Clayton [ Mon Jan 15, 2007 11:46 am ]
Post subject:  Re: Something important C++ is missing

(I think) what wtd is trying to get across here is that, if you only have one parameter, and you set it to a default value, then there's really no point in calling that function when you have no parameters to pass it. Instead, you overload the function so that you can just call it without an argument. In Common Lisp however, you can call it with or without an argument and it will figure out if the argment is on the default value or not, and acts accordingly. It's basically a DRY issue.

Author:  r.3volved [ Mon Jan 15, 2007 12:47 pm ]
Post subject:  Re: Something important C++ is missing

I would think you could pull off the same thing with C++ using a default value (or ideally defaulting to a constant to get rid of any hard coded values)

code:

CONST string DEFAULT_BAR = "bar";
...
void foo( string bar = DEFAULT_BAR )
{
   if( bar == DEFAULT_BAR ) cout << "I'll have to assume: ";
   cout << bar << endl;
}

Author:  wtd [ Mon Jan 15, 2007 1:45 pm ]
Post subject:  RE:Something important C++ is missing

And what if the user actually explicitly calls:

code:
foo("bar")


Smile

Author:  r.3volved [ Mon Jan 15, 2007 2:14 pm ]
Post subject:  Re: Something important C++ is missing

In which case, I would compare pointer addresses instead of comparing strings

code:

CONST string* DEFAULT_BAR = "bar";
...
void foo( string* bar = DEFAULT_BAR )
{
   if( bar == DEFAULT_BAR ) cout << "I'll have to assume: ";
   cout << *bar << endl;
}

Author:  wtd [ Mon Jan 15, 2007 2:45 pm ]
Post subject:  RE:Something important C++ is missing

What about something like an integer value, which is not a pointer? Smile

Author:  r.3volved [ Mon Jan 15, 2007 3:05 pm ]
Post subject:  RE:Something important C++ is missing

hmm I see...the complexity here grows quite a bit if you're making this as general as possible.

So in your lisp code there, it doesn't matter what kind of datatype you're passing to the function?
Does lisp use datatypes the same as C++, or is it more like a php datatype??

My first thoughts would be to template the function, but that would require the default value to change depending on the datatype...however if you're passing an integer, then it can never equal your constant string anyway, so why not cast to a string pointer or pass by string reference.

I guess in this case it doesn't matter too much if you're simply outputting the variable, but I could see issues if you're actually using the data for something datatype dependant.

However, I'm pretty certain a little bit of template metaprogramming can bypass this issue, but it would sadly require some pretty complicated coding determining types and values in your templates. Not to mention it would probably only compile on the newest VC++ compiler (I don't know how caught up GCC is on templating)

Author:  Clayton [ Mon Jan 15, 2007 3:49 pm ]
Post subject:  Re: Something important C++ is missing

which brings us to the point wtd was trying to make in his original post. C++ is missing this very important ability.

Author:  wtd [ Mon Jan 15, 2007 4:12 pm ]
Post subject:  RE:Something important C++ is missing

Yes, Common Lisp is dynamically typed, so bar can be a value of any type (it is by default a string). However, this is not pertinent to the point the post was trying to make.

Author:  Craige [ Mon Jan 15, 2007 4:58 pm ]
Post subject:  Re: Something important C++ is missing

Freakman @ Mon Jan 15, 2007 11:46 am wrote:
(I think) what wtd is trying to get across here is that, if you only have one parameter, and you set it to a default value, then there's really no point in calling that function when you have no parameters to pass it. Instead, you overload the function so that you can just call it without an argument. In Common Lisp however, you can call it with or without an argument and it will figure out if the argment is on the default value or not, and acts accordingly. It's basically a DRY issue.


Yeah, I'm sorry. I'm afraid I still don't get the point.

Author:  wtd [ Mon Jan 15, 2007 5:10 pm ]
Post subject:  RE:Something important C++ is missing

It's not just that it discerns whether or not the value supplied is the default value. It allows you, when writing the function body, to take into account whether or not the user supplied any value.

Author:  md [ Mon Jan 15, 2007 7:46 pm ]
Post subject:  RE:Something important C++ is missing

I have written code that does need to know if the callee provided the parameter or if it was the default. Fortunately my code can get away with doing the same things if the user explicitly passed the default as when the parameter wasn't passed.

But boy do I wish that I could see if a parameter was passed or if it's using the default.

Author:  Null [ Mon Jan 15, 2007 10:09 pm ]
Post subject:  Re: RE:Something important C++ is missing

wtd @ Mon Jan 15, 2007 5:10 pm wrote:
It's not just that it discerns whether or not the value supplied is the default value. It allows you, when writing the function body, to take into account whether or not the user supplied any value.


Could you please provide a practical piece of code where knowing whether there was a value passed or not is essential? Smile

I'm curious, because I'm having trouble thinking of one on my own.

Author:  wtd [ Mon Jan 15, 2007 10:20 pm ]
Post subject:  RE:Something important C++ is missing

Think of any time in C++ when you would write:

code:
some_type foo(some_other_type arg)
{
  // ...
}

some_type foo()
{
   // Not just:
   // return foo(some_default_value);
}


Where such a pattern is employed, the bodies are clearly taking different actions based on whether or not the user provides a value. Otherwise, a default parameter value would be employed.

Author:  ericfourfour [ Mon Jan 15, 2007 10:20 pm ]
Post subject:  RE:Something important C++ is missing

Maybe when using constructors, you want to have multiple ways of initializing the object.

Author:  md [ Tue Jan 16, 2007 1:43 am ]
Post subject:  RE:Something important C++ is missing

How about this: you're sending a UDP packet over a socket. The socket is bound to a port but you can send on any port. So you want to allow the user to specify a port to send on, and use the bound port if they don't. You either have to write a function which takes a port AND a function which doesn't and passes the bound port number to the first function; or find some default value which isn't a port (zero in this case).

A real world example.

Author:  abcdefghijklmnopqrstuvwxy [ Wed Jan 17, 2007 2:48 am ]
Post subject:  RE:Something important C++ is missing

So that fact that you can easily solve the aforementioned issue with function overloading makes it detrimental to the integrity of the C++ language? Forgive me, but if the user supplies a value that equals the default value there would be no reason to test if the user supplied the default or not.

Author:  md [ Wed Jan 17, 2007 12:55 pm ]
Post subject:  RE:Something important C++ is missing

No, the fact that one must resort to function overloading to know if the parameter is the default or if it was actually passed is the weakness. Not detrimental to the integrity of the language; but it's a big important feature that is missing and would be great to have.

And yes, usually if the user provides the same value as the default it doesn't matter. However I'm sure it's possible to think of a case where it would matter.

Author:  abcdefghijklmnopqrstuvwxy [ Wed Jan 17, 2007 11:50 pm ]
Post subject:  Re: Something important C++ is missing

why is function overloading so bad?

Author:  wtd [ Thu Jan 18, 2007 12:41 am ]
Post subject:  RE:Something important C++ is missing

It's not.

Author:  avok23 [ Sun May 04, 2008 3:43 am ]
Post subject:  Re: RE:Something important C++ is missing

code:
some_type foo(some_other_type arg = NULL)
{
  if(args == NULL)
  {
     //do default
  }
  else
  {}
}


Very Happy

BTW c++ is the greatest language ever

Author:  wtd [ Sun May 04, 2008 11:22 am ]
Post subject:  RE:Something important C++ is missing

What if the programmer supplied a value, but that value was NULL?

Author:  avok23 [ Mon May 05, 2008 8:48 pm ]
Post subject:  Re: RE:Something important C++ is missing

wtd @ Sun May 04, 2008 11:22 am wrote:
What if the programmer supplied a value, but that value was NULL?


i just wonder why any one would do that. What kind of function would require that?

Author:  r691175002 [ Mon May 05, 2008 10:21 pm ]
Post subject:  Re: RE:Something important C++ is missing

avok23 @ Mon May 05, 2008 8:48 pm wrote:
i just wonder why any one would do that. What kind of function would require that?

There has been two pages of discussion in this thread on that topic.

I personally don't see this as too much of a failing of C++. The situations that have been presented where this would be useful seem contrived and work fine with a sentinel value.

More importantly, if the programmer supplies default argument, I don't see why it is necessary to go around their back and have the function act differently than if they just let the function provide the same value.

If I see a function with the signature void foo (int bar = 0); I expect it to work the same whenever it is provided with a 0, no matter where the 0 comes from.

Author:  md [ Tue May 06, 2008 10:01 am ]
Post subject:  Re: RE:Something important C++ is missing

r691175002 @ 2008-05-05, 10:21 pm wrote:
avok23 @ Mon May 05, 2008 8:48 pm wrote:
i just wonder why any one would do that. What kind of function would require that?

There has been two pages of discussion in this thread on that topic.

I personally don't see this as too much of a failing of C++. The situations that have been presented where this would be useful seem contrived and work fine with a sentinel value.

More importantly, if the programmer supplies default argument, I don't see why it is necessary to go around their back and have the function act differently than if they just let the function provide the same value.

If I see a function with the signature void foo (int bar = 0); I expect it to work the same whenever it is provided with a 0, no matter where the 0 comes from.


To go back to my socket example, there are two options: sending on a specific port and sending on the default port. The default port is socket specific - you decide it when you open the socket - and cannot be used as a default parameter to any function. So, one of the normal ports must now be used to indicate using the default port - or a function must be overloaded (and with generic virtual base classes and templates... that's a pain).

Author:  matt271 [ Fri May 06, 2011 6:37 pm ]
Post subject:  Re: Something important C++ is missing

i think if you intend to have a different function for when no argument(or a lack of one or more) is/are provided, and a function for when an argument(or 'all' of them) is/are provided; then this is the time u should use overloading...

is that not what its for?

Author:  Insectoid [ Fri May 06, 2011 6:48 pm ]
Post subject:  RE:Something important C++ is missing

Check the timestamps matt. You're a few years late.

I think it would be interesting if you could call a function that takes, say, a pointer, an integer and a float in the following manner:

code:
int foo (int*, int, float){
    //stuff
}

cout << foo (int*, some_int, float);


This would specify exactly which function you're calling, allowing for passing in only some parameters without ambiguous overloading.

This would not allow you to change the behavior of the function based on which variables were passed, however if you really want to do that you might as well just overload it.


: