Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Why is not defined operator+ for vector?
Why is not defined operator+  for vector?

For instance,
vector<T> v1, v2, v3;
T a;

v1=v2+a;
v1=v2+v3;


--
Alex Vinokur
mailto:alexvn@connect.to
http://mathforum.org/library/view/10978.html










Report this thread to moderator Post Follow-up to this message
Old Post
Alex Vinokur
05-19-04 02:32 PM


Re: Why is not defined operator+ for vector?
Alex Vinokur wrote:
> Why is not defined operator+  for vector?

What would it mean?

> For instance,
> vector<T> v1, v2, v3;
> T a;
>
> v1=v2+a;

Does this make v1 the concatenation of v2 and a, or the result of adding
a to each element of v2?

> v1=v2+v3;

Does this add each element of v2 to the corresponding element from v3,
or make v1 the concatenation of v2 and v3?

Report this thread to moderator Post Follow-up to this message
Old Post
Jeff Schwab
05-19-04 02:32 PM


Re: Why is not defined operator+ for vector?
"Jeff Schwab" <jeffplus@comcast.net> wrote in message news:6qadnT1f2afbxDbdRVn-vA@comcast.c
om...
> Alex Vinokur wrote: 
>
> What would it mean?
> 
>
> Does this make v1 the concatenation of v2 and a, or the result of adding
> a to each element of v2?
Concatenation of v2 and a.

> 
>
> Does this add each element of v2 to the corresponding element from v3,
> or make v1 the concatenation of v2 and v3?
Concatenation of v2 and v3.


--
Alex Vinokur
mailto:alexvn@connect.to
http://mathforum.org/library/view/10978.html




Report this thread to moderator Post Follow-up to this message
Old Post
Alex Vinokur
05-19-04 02:32 PM


Re: Why is not defined operator+ for vector?
> Why is not defined operator+  for vector?
>
> For instance,
> vector<T> v1, v2, v3;
> T a;
>
> v1=v2+a;
> v1=v2+v3;

Too many operators make code less readable. If operator's meaning is not
obvious in context of the operation it performs, it's better to replace it
with named function. In case of standard library containers these functions
were named push_back and insert, which IMO better describes append
operations than operator +.

// v1 = v2 + a becomes:
v1 = v2;
v2.push_back(a);

// v1 = v2 + v3 becomes:
v1 = v2;
v1.insert(v1.end(), v3.begin(), v3.end());

cheers,
Marcin




Report this thread to moderator Post Follow-up to this message
Old Post
Marcin Kalicinski
05-19-04 03:37 PM


Re: Why is not defined operator+ for vector?
Alex Vinokur wrote:
> "Jeff Schwab" <jeffplus@comcast.net> wrote in message news:6qadnT1f2afbxDb
dRVn-vA@comcast.com...
> 
... 
>
> Concatenation of v2 and a.
... 
>
> Concatenation of v2 and v3.

This behavior differs from the traditional, mathematical meaning of
adding two vectors.  Neither concatenation, nor the mathematical meaning
are "wrong."  Since there is no single, obvious meaning of operator+ for
vectors, why should any one definition be part of the standard library?
It can be overloaded easily in client code.

Report this thread to moderator Post Follow-up to this message
Old Post
Jeff Schwab
05-19-04 03:37 PM


Re: Why is not defined operator+ for vector?
Alex Vinokur 2004-05-19 :

> "Jeff Schwab" <jeffplus@comcast.net> wrote in message news:6qadnT1f2afbxDb
dRVn-vA@comcast.com... 
> Concatenation of v2 and a.

I would have said it adds a to every element of v2
 
> Concatenation of v2 and v3.

I would have said it adds v2 and v3 element by element.

So you see a good reason (among others) why that operator is
*not* defined...
ciao
Walter

Report this thread to moderator Post Follow-up to this message
Old Post
Walter Tross
05-19-04 03:37 PM


Re: Why is not defined operator+ for vector?
Jeff Schwab wrote:
>
> Alex Vinokur wrote: 
>
> What would it mean?

This is a weak argument.  There are plenty of methods/operators that are far
from intuitive.

Take vector::empty(), for example.

Without prior (stl) knowledge, there is no way to know what this means: does
 it
test for an empty condition or empty the contents of the vector?

> 
>
> Does this make v1 the concatenation of v2 and a, or the result of adding
> a to each element of v2?
> 
>
> Does this add each element of v2 to the corresponding element from v3,
> or make v1 the concatenation of v2 and v3?

Report this thread to moderator Post Follow-up to this message
Old Post
Julie
05-19-04 03:37 PM


Re: Why is not defined operator+ for vector?
Julie wrote:
> Jeff Schwab wrote:
> 
>
>
> This is a weak argument.

It's not an argument.  It's a question.

> There are plenty of methods/operators that are far
> from intuitive.

And more would help?

> Take vector::empty(), for example.
>
> Without prior (stl) knowledge, there is no way to know what this means: do
es it
> test for an empty condition or empty the contents of the vector?

You have a good point; a name like "is_empty" might have been better.

Report this thread to moderator Post Follow-up to this message
Old Post
Jeff Schwab
05-19-04 03:37 PM


Re: Why is not defined operator+ for vector?
Julie wrote:
>
> Jeff Schwab wrote: 
>

Preamble: my previous response wasn't meant to be attacking or argumentative
.
I don't disagree that specifically _operator_+_ is ambiguous and not warrant
ed
for various reasons pointed out by other respondents.

> This is a weak argument.  There are plenty of methods/operators that are f
ar
> from intuitive.
>
> Take vector::empty(), for example.
>
> Without prior (stl) knowledge, there is no way to know what this means: do
es it
> test for an empty condition or empty the contents of the vector?
> 

Report this thread to moderator Post Follow-up to this message
Old Post
Julie
05-19-04 04:31 PM


Re: Why is not defined operator+ for vector?
Jeff Schwab wrote:
>
> Julie wrote: 
>
> It's not an argument.  It's a question.

Yep, you are absolutely correct.  I wasn't thinking clearly at all when I
originally responded -- you can safely ignore it.

> 
>
> And more would help?

ibid.

> 
>
> You have a good point; a name like "is_empty" might have been better.

Report this thread to moderator Post Follow-up to this message
Old Post
Julie
05-19-04 05:32 PM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

C++ archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:26 AM.

 

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.