Topic: Using STL algorithms as member functions.


Author: "sebor@roguewave.com" <sebor@roguewave.com>
Date: Wed, 23 May 2007 00:21:45 CST
Raw View
On May 10, 4:42 pm, german diago <germandi...@gmail.com> 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 think the trend is actually in the opposite direction, i.e., to
"transform" ordinary function calls into member function calls when
such transformations make sense. I.e., trim(s) would be transformed
into s.trim() in a "concept-enabled" template.

---
[ 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                      ]





Author: "sebor@roguewave.com" <sebor@roguewave.com>
Date: Tue, 22 May 2007 12:16:43 CST
Raw View
On May 10, 4:42 pm, german diago <germandi...@gmail.com> 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 think the trend is actually in the opposite direction, i.e., to
"transform" ordinary function calls into member function calls when
such transformations make sense. I.e., trim(s) would be transformed
into s.trim() in a "concept-enabled" template.

---
[ 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                      ]





Author: german diago <germandiago@gmail.com>
Date: Thu, 10 May 2007 16:42:27 CST
Raw View
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                      ]





Author: e0226430@stud3.tuwien.ac.at (Sebastian Redl)
Date: Fri, 11 May 2007 00:12:05 GMT
Raw View

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-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Roman.Perepelitsa@gmail.com" <Roman.Perepelitsa@gmail.com>
Date: Sun, 13 May 2007 09:55:24 CST
Raw View
===================================== 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                      ]





Author: german diago <germandiago@gmail.com>
Date: Sun, 13 May 2007 09:56:19 CST
Raw View
On 11 mayo, 02:12, e0226...@stud3.tuwien.ac.at (Sebastian Redl) wrote:
> 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                      ]