471,066 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

Extract function result type

Hi everybody!

While trying to implement a generic sorting function which takes a
member function(on which base the actual sort happens) as a parameter,
I have met the problem that I need to use the result type of the member
function and as I have understood there is no way in standart stl to
extract it. While searching through this groop and in general, I could
found some references to the problem in the following topic :

q=extract+return+type+function&rnum=1#d1f20e844962 2005

and for example the reference on TR1 from Scott Meyers "Effective C++"
http://aristeia.com/EC3E/TR1_info_frames.html. Where exists the class
"result_of" which takes the function as a template parameter and to get
the type is simply the following: result_of<function>::type.

Does anybody know how far is TR1 integrated in stl and whether and how
the above functionaliry can be used,
or does boost or any other enhanced library have the above feature?

In order to show an idea where the generic extraction of function
result type can be used, there is an example with generic sorting
Imagine we have a class Element:
class Element {
typedef int element_type;
typedef string name_type;
Element(const name_type& name, element_type a, element_type b) :
_name(name), _a(a), _b(b) {}
const name_type& getName() const { return _name; }
element_type getA() const { return _a; }
element_type getB() const { return _b; }
~Element() { }
name_type _name;
element_type _a, _b;
and class Alphabet which is a kind of container for Elements:
class Alphabet {
typedef Element element_type;
typedef vector<element_type> container;
typedef container::size_type size_type;
Alphabet(const container& elements) : _elements(elements) {}
~Alphabet() {}

size_type size() const { return _elements.size(); }

template <typename T>
void sorting(T func_ptr); // the main point of interest
container _elements;
}; // an example has minimal interface, using namespace ... are

ostream& operator << (ostream& os, const Alphabet& alphabet) {
for (Alphabet::size_type i = 0; i < alphabet.size(); ++i ) {
Alphabet::element_type element = alphabet.at(i);
os << element.getName() << ", " << element.getA() << ", " <<
element.getB() << endl;
return os;

// definition of Alphabet::sorting is further below
The idea is to sort elements in Alphabet by giving an Element member
function as a sorting criteria. // another way would be to create
functors with different comparison function, but that what I would like
to avoid at the moment and see if this way could be elegant...

The example of main would be like this:
int main(int argc, char** argv) {

Element a("a", 2, 4), b("b", 3, 2), c("c", 4, 1), d("d", 1, 3);
vector<Element> els; // elements
els.push_back(a); els.push_back(b); els.push_back(c);

Alphabet alphabet(els);

cout << "Alphabet before sort: " << alphabet << endl;
cout << "Alphabet sorted on name: " << alphabet << endl;
cout << "Alphabet sorted on A: " << alphabet << endl;
cout << "Alphabet sorted on B: " << alphabet << endl;
return 0;

Which prints out elements in the alphabet depending on the sorting

So the definition of sorting would be lke this (with the help of TR1

template <typename T>
void Alphabet::sorting(T func_ptr) {
sort(_elements.begin(), _elements.end(),
less<typename tr1::result_of(func_ptr)::type>(),

where compose_f_gx_hy is taken from here:
The following code example is taken from the book
The C++ Standard Library - A Tutorial and Reference
by Nicolai M. Josuttis, Addison-Wesley, 1999
Copyright Nicolai M. Josuttis 1999

#include <functional>
/* class for the compose_f_gx_hy adapter
template <class OP1, class OP2, class OP3>
class compose_f_gx_hy_t
: public std::binary_function<typename OP2::argument_type,
typename OP3::argument_type,
typename OP1::result_type>
OP1 op1; // process: op1(op2(x),op3(y))
OP2 op2;
OP3 op3;
// constructor
compose_f_gx_hy_t (const OP1& o1, const OP2& o2, const OP3& o3)
: op1(o1), op2(o2), op3(o3) {

// function call
typename OP1::result_type
operator()(const typename OP2::argument_type& x,
const typename OP3::argument_type& y) const {
return op1(op2(x),op3(y));
/* convenience function for the compose_f_gx_hy adapter
template <class OP1, class OP2, class OP3>
inline compose_f_gx_hy_t<OP1,OP2,OP3>
compose_f_gx_hy (const OP1& o1, const OP2& o2, const OP3& o3) {
return compose_f_gx_hy_t<OP1,OP2,OP3>(o1,o2,o3);
(this function should be also in boost, but I haven't checked which

Do you know whether such Alphabet::sorting definition works, and if yes
there is more beginner question: how to intergrate TR1 in the project?
Or is there something similar in boost library?

By the way, I have already tried to implement Alphabet::sorting without
templates (say for only a certain types of function, in the above
example there were : Element::getA and Element::getB) and that worked
just fine. Would be nice to get more generic solution working.

Any ideas are appreciated.

kind regards,
Anton Pervukhin

Nov 8 '05 #1
1 2383
result_of is part of the boost utility library.


-- peter

Nov 8 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Ren | last post: by
7 posts views Thread by William Payne | last post: by
10 posts views Thread by Robert Schultz | last post: by
9 posts views Thread by Brian Hanson | last post: by
3 posts views Thread by Beta What | last post: by
reply views Thread by napolpie | last post: by
2 posts views Thread by clai83 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.