C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using STL algorithms as member functions.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
german diago
Guest





PostPosted: Thu May 10, 2007 10:42 pm    Post subject: Using STL algorithms as member functions. Reply with quote



Hello. I've been thinking about the use of STL and some libraries from
other languages and its usage patterns.
In C++ you've got some data structures and then, the algorithms are
used from outside the class, which sometimes, is less natural than
approaches having algorithms inside classes.
In languages like Ruby or C#, you've got the algorithms inside the
classes.


I think it would be a very good adittion to the standard to be able to
reuse code from the STL (and other classes with such a style) inside a
class of a user-defined type as a member function or even to be able
to reuse it from anywhere.


The idea is the following: if your class meets the requirements of a
concept, then, the free function can be used as a member function. The
mapping would be that the first argument of the free function would be
*this inside the class.

You could do the following.

///This is a free function
namespace algo {

template<String S>
void trim(S s) {
...
}

//UpperCase howmany characters
template<String S>
void upper(std::size_t howmany) {
...
}

//UpperCase
template<String S>
void upper(S s)
{
...
}
}

//Meets string requirements.
class mystring {
public:
using algo::trim();

using algo::upper(std::size_t);
};

Then, you could do:

mystring s(" hello world!");
s.trim();

The syntax is the first thing I thought about, but you get the idea.
When the code is generated, the first argument passed to the function
is mystring and the code generated is the one for that class. With
this mapping, you have to write minimal code to add free algorithms to
your classes and it would be very easy to add a set of already
implemented functions to your class.
And with the overloading rules, the compiler could pick up the most
refined concept to generate code, generating always the best code for
your class, and without having class hierarchies with virtual
functions. You get the benefits of object-oriented programming with
optimal speed.
What do you think about the idea?

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Sebastian Redl
Guest





PostPosted: Fri May 11, 2007 5:12 am    Post subject: Re: Using STL algorithms as member functions. Reply with quote



On Thu, 10 May 2007, german diago wrote:

Quote:
The idea is the following: if your class meets the requirements of a
concept, then, the free function can be used as a member function. The
mapping would be that the first argument of the free function would be
*this inside the class.
What do you think about the idea?


I thought about the same thing for a long time and finally decided that it
was a bad idea.

First, "natural syntax" is just a matter of taste and what you're used to.
I have no issues at all with the free function sytax.

Second, the name lookup rules are already so complex, this would make it
even weirder. For every member syntax function, you would now have to
look:

- in the class (possibly templated or even a specialization)
- in the base classes (possibly templated or even a specialization)
- in the current namespace plus all ancestor namespaces for a non-member
function, possibly templated.
- in the namespace of the class for a non-member function, possibly
templated.
- in the namespace of any other argument for a non-member function,
possibly templated.

Third, depending on how the new rules work, it might break existing code.


In conclusion, the extension comes at some additional cost, for very
dubious gain.

Sebastian Redl

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Roman.Perepelitsa@gmail.c
Guest





PostPosted: Sun May 13, 2007 3:55 pm    Post subject: Re: Using STL algorithms as member functions. Reply with quote



===================================== MODERATOR'S COMMENT:

When replying to posts, please do quote enough context for your message
to make sense without needing to see other messages.

------=_Part_163108_22954690.1179068111649
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

approve<br>comment<br>When replying to posts, please do quote enough context for your message<br>to make sense without needing to see other messages.<br><br>

------=_Part_163108_22954690.1179068111649--


===================================== END OF MODERATOR'S COMMENT
There is no practical benefit in your proposal; it's just a syntax
sugar. And you know, "s.trim()" is one character longer than
"trim(s)" ;-)

Roman Perepelitsa.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
german diago
Guest





PostPosted: Sun May 13, 2007 3:56 pm    Post subject: Re: Using STL algorithms as member functions. Reply with quote

On 11 mayo, 02:12, e0226...@stud3.tuwien.ac.at (Sebastian Redl) wrote:
Quote:
On Thu, 10 May 2007, german diago wrote:
The idea is the following: if your class meets the requirements of a
concept, then, the free function can be used as a member function. The
mapping would be that the first argument of the free function would be
*this inside the class.
What do you think about the idea?

I thought about the same thing for a long time and finally decided that it
was a bad idea.

First, "natural syntax" is just a matter of taste and what you're used to.
I have no issues at all with the free function sytax.

Second, the name lookup rules are already so complex, this would make it
even weirder. For every member syntax function, you would now have to
look:

- in the class (possibly templated or even a specialization)
- in the base classes (possibly templated or even a specialization)
- in the current namespace plus all ancestor namespaces for a non-member
function, possibly templated.
- in the namespace of the class for a non-member function, possibly
templated.
- in the namespace of any other argument for a non-member function,
possibly templated.

Third, depending on how the new rules work, it might break existing code.

In conclusion, the extension comes at some additional cost, for very
dubious gain.

Sebastian Redl

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-...@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ:http://www.comeaucomputing.com/csc/faq.html ]

Do you really think this would affect the rules?
You can add free functions as member functions (just the ones you
explicitly add). For example, in the example above, it can be added
algo::trim() (and just that). The compiler would try to find the best
concept-based match in that namespace, and just in that one. I don't
understand (forgive my ignorance, I'm not a C++ expert) how this could
have side effects with the overloading rules. It does not change them
at all. It's just syntactic sugar to use the functions in a more
natural way.
I think that defining new types should be as fast as possible in any
language, and this pushes code reuse and minimal rewriting and
rewrapping of free functions.
Anyway, maybe I'm wrong :-)

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.