Topic: Pointer to function call operator syntax question
Author: george@dircon.co.uk (George Curry)
Date: 1997/07/28 Raw View
In article <5r7of2$348$1@newsserver.dircon.co.uk>, george@dircon.co.uk (George Curry) wrote:
>I have just started reading Musser and Saini's STL Tutorial and Reference
>Guide. I got as far as example 2-11, before getting confused. The example is
>reproduced below, with the necessary changes to use ANSI STL.
>
>// Using the generic accumulate algorithm to compute a product,
>// using a function object.
>
>#include <vector>
>#include <numeric>
>#include <assert.h>
>
>using namespace std;
>
>class multiply {
> public:
> int operator()(int x, int y) const { return x * y; }
>};
>
>int main() {
> cout << "Using generic accumulate algorithm to "
> << "compute a product." << endl;
> // Initialize vector1 to x[0] through x[4]:
> int x[5] = {2, 3, 5, 7, 11};
> vector<int> vector1(x, x+5);
> int product = accumulate(
> vector1.begin(), vector1.end(), 1, multiply());
> assert(product == 2310);
>}
>
>My problem relates to the following line:
>
> int product = accumulate(
> vector1.begin(), vector1.end(), 1, multiply());
>
>specifically to the part
>
> multiply()
>
>>From what I can make out this is passing a pointer to the operator () function
>
>for class multiply. I am however unable to find how the syntax for this is
>derived. I have copies of C++ Programming Language (2nd Edition), Design and
>Evolution of C++, the Annotate Reference Manual, and the Draft C++ standard in
>PDF form, but I can't find any examples using this format.
>
>If anyone can point me in the right direction on this I would appreciate it.
>
Thanks to Srinivas Vobilisetti and Andrei Smirnov, who explained to me that
multiply() is the same as multiply::multiply(), i.e. the default constructor
for a multiply object. What amazes me about this is that I received replies
about this days before the article reached my home news feed... and an article
I posted later came up before it!
George Curry
george@dircon.co.uk
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: heh@beamtech.ANTISPAM.com (Howard E. Hinnant)
Date: 1997/07/30 Raw View
In article <5r7of2$348$1@newsserver.dircon.co.uk>, george@dircon.co.uk
(George Curry) wrote:
>
> My problem relates to the following line:
>
> int product = accumulate(
> vector1.begin(), vector1.end(), 1, multiply());
>
> >From what I can make out this is passing a pointer to the operator ()
function
> for class multiply. I am however unable to find how the syntax for this is
> derived. I have copies of C++ Programming Language (2nd Edition), Design and
Actually you are passing an instance of the class multiply using the
default constructor. accumulate will no doubt take this argument and
apply the () operator to it:
template <class In, class T, class BinOp>
T accumulate(In first, In last, T init, BinOp op)
{
while (first != last)
init = op(init, *first++);
return init;
}
I have been reading Stroustrup's latest: "The C++ Programming Language,
3rd Edition." It has more than its share of typos, but highly recommended
nevertheless. The above routine is quoted from chapter 22 "Numerics"
which delves into valarray, slicing, complex, etc.
-Howard
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: george@dircon.co.uk (George Curry)
Date: 1997/07/24 Raw View
I have just started reading Musser and Saini's STL Tutorial and Reference
Guide. I got as far as example 2-11, before getting confused. The example is
reproduced below, with the necessary changes to use ANSI STL.
// Using the generic accumulate algorithm to compute a product,
// using a function object.
#include <vector>
#include <numeric>
#include <assert.h>
using namespace std;
class multiply {
public:
int operator()(int x, int y) const { return x * y; }
};
int main() {
cout << "Using generic accumulate algorithm to "
<< "compute a product." << endl;
// Initialize vector1 to x[0] through x[4]:
int x[5] = {2, 3, 5, 7, 11};
vector<int> vector1(x, x+5);
int product = accumulate(
vector1.begin(), vector1.end(), 1, multiply());
assert(product == 2310);
}
My problem relates to the following line:
int product = accumulate(
vector1.begin(), vector1.end(), 1, multiply());
specifically to the part
multiply()
>From what I can make out this is passing a pointer to the operator () function
for class multiply. I am however unable to find how the syntax for this is
derived. I have copies of C++ Programming Language (2nd Edition), Design and
Evolution of C++, the Annotate Reference Manual, and the Draft C++ standard in
PDF form, but I can't find any examples using this format.
If anyone can point me in the right direction on this I would appreciate it.
George Curry
george@dircon.co.uk
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: jbuck@synopsys.com (Joe Buck)
Date: 1997/07/25 Raw View
george@dircon.co.uk (George Curry) writes:
>My problem relates to the following line:
> int product = accumulate(
> vector1.begin(), vector1.end(), 1, multiply());
>specifically to the part
> multiply()
> From what I can make out this is passing a pointer to the operator ()
> function for class multiply.
(The standard refers to "multiplies", by the way).
No, it is not. multiply is a class. A class name followed by an argument
list is a constructor call (in this case the list is empty). This is
creating a temporary object of class multiply and passing it to the accumulate
function template. That template may look like
template <class InputIterator, class T, class BinaryOperation>
T accumulate(InputIterator first, InputIterator last, T init,
BinaryOperation binary_op) {
while (first != last)
init = binary_op(init, *first++);
return init;
}
So the variable binary_op is of type multiply. The body of the loop calls
the operator() of the object. Either a function object or a pointer to
function will do here, but in this case there is no pointer to function
involved.
--
-- Joe Buck http://www.synopsys.com/pubs/research/people/jbuck.html
Help stamp out Internet spam: see http://spam.abuse.net/spam/
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Andrei Smirnov <andreis@InterWorld.com>
Date: 1997/07/28 Raw View
george@dircon.co.uk (George Curry) wrote:
... skipped ...
> int main() {
> cout << "Using generic accumulate algorithm to "
> << "compute a product." << endl;
> // Initialize vector1 to x[0] through x[4]:
> int x[5] = {2, 3, 5, 7, 11};
> vector<int> vector1(x, x+5);
> int product = accumulate(
> vector1.begin(), vector1.end(), 1, multiply());
> assert(product == 2310);
> }
>
> My problem relates to the following line:
>
> int product = accumulate(
> vector1.begin(), vector1.end(), 1, multiply());
multiply() creates temp object of class multiply using default
constructor. You can put it other way:
multiply m;
int product = accumulate(
vector1.begin(), vector1.end(), 1, m);
It has the same effect like the example above. Default ctor is generated
for class multiply.
> From what I can make out this is passing a pointer to the operator () function
> for class multiply.
No, it's just passing of object of class multiply by const reference. I
think you have already guess.
> If anyone can point me in the right direction on this I would appreciate it.
Hope, it helps.
> George Curry
> george@dircon.co.uk
Andrei Smirnov, andreis@interworld.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]