473,763 Members | 7,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 :

http://groups.google.com/group/comp.lang.c++/
browse_thread/thread/78677a3723145f8 8/d1f20e844962200 5?
q=extract+retur n+type+function &rnum=1#d1f20e8 449622005

and for example the reference on TR1 from Scott Meyers "Effective C++"
site:
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<funct ion>::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
function:
Imagine we have a class Element:
--------------------------------------------
class Element {
public:
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() { }
private:
name_type _name;
element_type _a, _b;
};
--------------------------------------------
and class Alphabet which is a kind of container for Elements:
--------------------------------------------
class Alphabet {
public:
typedef Element element_type;
typedef vector<element_ type> container;
typedef container::size _type size_type;
Alphabet(const container& elements) : _elements(eleme nts) {}
~Alphabet() {}

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

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

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

// definition of Alphabet::sorti ng 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 );
els.push_back(d );

Alphabet alphabet(els);

cout << "Alphabet before sort: " << alphabet << endl;
alphabet.sortin g(&Element::get Name);
cout << "Alphabet sorted on name: " << alphabet << endl;
alphabet.sortin g(&Element::get A);
cout << "Alphabet sorted on A: " << alphabet << endl;
alphabet.sortin g(&Element::get B);
cout << "Alphabet sorted on B: " << alphabet << endl;
return 0;
}

Which prints out elements in the alphabet depending on the sorting
criteria.

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

template <typename T>
void Alphabet::sorti ng(T func_ptr) {
sort(_elements. begin(), _elements.end() ,
compose_f_gx_hy (
less<typename tr1::result_of( func_ptr)::type >(),
mem_fun_ref(fun c_ptr),
mem_fun_ref(fun c_ptr)));
}

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_fun ction<typename OP2::argument_t ype,
typename OP3::argument_t ype,
typename OP1::result_typ e>
{
private:
OP1 op1; // process: op1(op2(x),op3( y))
OP2 op2;
OP3 op3;
public:
// 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_typ e
operator()(cons t typename OP2::argument_t ype& x,
const typename OP3::argument_t ype& 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
one)

Do you know whether such Alphabet::sorti ng 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::sorti ng 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 2505
result_of is part of the boost utility library.

http://www.boost.org/libs/utility/utility.htm#result_of

-- peter

Nov 8 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3173
by: Ren | last post by:
Suppose I have a file containing several lines similar to this: :10000000E7280530AC00A530AD00AD0B0528AC0BE2 The data I want to extract are 8 hexadecimal strings, the first of which is E728, like this: :10000000 E728 0530 AC00 A530 AD00 AD0B 0528 AC0B E2 Also, the bytes in the string are reversed. The E728 needs to be 28E7,
7
3607
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For example, consider this four-bit number: 1100 from this number I want to extract two numbers: 1000 and 100 had the four-bit number been 0101 I would want to extract 100 and 1. How should I do this? I wish I had some code to post but I don't...
10
8657
by: Robert Schultz | last post by:
I have a C/C++ file that I simply want to 'extract' a function from. Something like: extract <function name> <c or cpp file> I want it to return from the beginning of the function, to the end. I tried cscope, and that will find the function, but it won't tell me how many lines it is or extract it for me. Any ideas on what I could use?
9
3393
by: Brian Hanson | last post by:
Hi, I have an unusual problem that just showed its ugly head at a pretty bad time. I have an asp.net (VB) app that takes data from an Excel sheet and puts it into SQL Server. I get the data out of Excel using OleDB, and suddenly, some of the data was not being extracted from Excel. I use OleDb for the extract into a DataTable and from there an SqlClient.SqlCommand to put it into SQL Server.
3
3653
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take arguments of their real types, then when I pass this pointer through an interface function, I...
6
2101
by: Ben | last post by:
Hi We have a Dataset that has been populated from the output parameter of a Stored Procedure (@Output). I understand that I can extract a single item when the dataset is populated by a table using this code: CType(objDataSet.Tables("MyTable").Rows(0).Item("MyField"), String)
0
2050
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by word and number arguments like this: GRILLE EURAT5 Coin Nord-Ouest : 46.50/ 0.50 Coin Sud-E Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file.
2
2312
by: parag_paul | last post by:
I was looking into the following page http://gcc.gnu.org/onlinedocs/gcc/Long-Long.html Here there is a term open-coded , why to ? And I saw the following definition for long long 5.8 Double-Word Integers ISO C99 supports data types for integers that are at least 64 bits wide, and as an extension GCC supports them in C89 mode and in C++. Simply write long long int for a signed integer, or unsigned long long
2
1697
by: clai83 | last post by:
mysql and mysqli functions always return strings values, and I understand that I can set the type of the data via the settype function AFTER I extract the data, but is there a way with PHP to extract the data as it is specified in the mysql database? (i.e if the type is an integer then PHP will insert the integer value in the result set). The reason I ask is I want to create a function to verify the integrity of the database before I process...
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.