From: Mathias Gaunard on
On Mar 31, 11:36 pm, "Thiago A." <thiago.ad...(a)gmail.com> wrote:
> Using function notation we have expressions like that:
>
> F(G(H(X)))
>
> [...]
>
> For instance, we could write:
>
> matrix.Rotate(pi).ReflectHor().Inverse();

This works fine, but the main problem is that it is intrusive: you
need to have such functions declared as members of the returned types.
Using operator overloading, you can avoid this issue.

Consider matrix | Rotate(pi) | ReflectHor() | Inverse()
Such syntax is used by the new extension to Boost.Range.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Thiago A. on

> But in your case you don't need to.
> Just have every algorithm function (like Rotate())
> return a reference to a (non-local) object that defines other needed
> functions, like ReflectHor()

The problem of member-function is to repeat the same algorithm in each
class, or to have to create a non-member function and call it from
each member-function. Consequently each class must know each
algorithm. Apart of that, sometimes you cannot change the class code,
like std::vector or the type is not a class.

For instance:
2.pow(3);

int operator . pow (int a, int b) { return pow(a, b); }




--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Thiago A. on

> > Using function notation we have expressions like that:
>
> > F(G(H(X)))
>
> > [...]
>
> > For instance, we could write:
>
> > matrix.Rotate(pi).ReflectHor().Inverse();
>
> This works fine, but the main problem is that it is intrusive: you
> need to have such functions declared as members of the returned types.
> Using operator overloading, you can avoid this issue.
>
> Consider matrix | Rotate(pi) | ReflectHor() | Inverse()
> Such syntax is used by the new extension to Boost.Range.

I agree.
In the matrix sample, the operator * could be used.
But considering generic samples the math operators can be VERY
confusing.
Maybe the general idea is to create �named� operators like "pow".
2 + 2 pow 3;
However in this case we should also declare the precedence and this
also would be very confusing.

I think that the dot operator would be simpler because the precedence
is the same we use in class members.
2 + 2.pow(3)


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: achp on
On Apr 1, 2:36 am, "Thiago A." <thiago.ad...(a)gmail.com> wrote:
> However in some cases, I would prefer to read expression from left to
> right applying the next function in the previous result.
>
> H(x) G() F()

I don't think you can have it that way.

However, you might choose to have it this way:

apply(x, H, G, F);

where

template<class T>
T apply(T x)
{
return std::forward<T>(x);
}

template<class T, class F1, class... Fn>
auto apply(T x, F1 f1, Fn... fn) -> decltype(apply(f1(x),
std::forward<Fn>(fn)...))
{
return apply(f1(x), std::forward<Fn>(fn)...);
}

Sorry for mistakes, I have not tested it, but you probably got the
idea.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Seungbeom Kim on
On 2010-04-01 07:08, Mathias Gaunard wrote:
> On Mar 31, 11:36 pm, "Thiago A."<thiago.ad...(a)gmail.com> wrote:
>>
>> For instance, we could write:
>>
>> matrix.Rotate(pi).ReflectHor().Inverse();
>
> This works fine, but the main problem is that it is intrusive: you
> need to have such functions declared as members of the returned types.
> Using operator overloading, you can avoid this issue.

This is a valid concern. Then how about defining one function
that can call arbitrary operations:

matrix.apply(Rotate, pi).apply(ReflectHor).apply(Inverse);

> Consider matrix | Rotate(pi) | ReflectHor() | Inverse()
> Such syntax is used by the new extension to Boost.Range.

I like this syntax, too!

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]