Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Something important C++ is missing
Index -> Programming, C++ -> C++ Tutorials
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: 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)))
Sponsor
Sponsor
Sponsor
sponsor
Null




PostPosted: 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
Clayton




PostPosted: 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.
wtd




PostPosted: 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.
r.3volved




PostPosted: 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?
wtd




PostPosted: 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.
Clayton




PostPosted: 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?
Craige




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: 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.
r.3volved




PostPosted: 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;
}
wtd




PostPosted: 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
r.3volved




PostPosted: 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;
}
wtd




PostPosted: 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
r.3volved




PostPosted: 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)
Clayton




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 3  [ 34 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: