473,385 Members | 1,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

tr1::function - why so many temporaries?

I believe boost::function is part of TR1, so imagine this is a job
interview question in 2009:

Guess how many times ~int_div() is called between "before" and "after"
and explain why. (I was surprised and still don't understand why) :
#include <iostream>
#include <boost/function.hpp>

typedef boost::function2<float, int, intfun;

struct int_div {
float operator()(int x, int y) const { return ((float)x)/y; }
~int_div() { std::cout << "destructor\n"; }
};

int main() {
int_div i;
fun f;
{
std::cout << "before\n";
f = i;
std::cout << "after\n";
}
}

Feb 3 '07 #1
2 1797
On Feb 2, 11:14 pm, n.torrey.pi...@gmail.com wrote:
I believe boost::function is part of TR1, so imagine this is a job
interview question in 2009:

Guess how many times ~int_div() is called between "before" and "after"
and explain why. (I was surprised and still don't understand why) :

#include <iostream>
#include <boost/function.hpp>

typedef boost::function2<float, int, intfun;

struct int_div {
float operator()(int x, int y) const { return ((float)x)/y; }
~int_div() { std::cout << "destructor\n"; }

};

int main() {
int_div i;
fun f;
{
std::cout << "before\n";
f = i;
std::cout << "after\n";
}

}
Well, the answer is more times than std::tr1:function does:

#include <tr1/functional>
#include <iostream>

typedef std::tr1::function< float(int, int)fun;

struct int_div
{
float operator()(int x, int y) const
{
return ((float)x)/y;
}
~int_div()
{
std::cout << "destructor\n";
}
};

int main()
{
int_div i;
fun f;

{
std::cout << "before\n";
f = i;
std::cout << "after\n";
}
}

Program Output:

before
destructor
destructor
after
destructor
destructor

Greg

Feb 3 '07 #2
n.************@gmail.com wrote:
I believe boost::function is part of TR1, so imagine this is a job
interview question in 2009:
Well, std::tr1::function is part of TR1. <g>
Guess how many times ~int_div() is called between "before" and "after"
and explain why. (I was surprised and still don't understand why) :
It depends on the implementation, and it's mostly unimportant. When I write

int i = 3;
cout << i << '\n';

how many times does the value of i get copied? Like ints, callable
objects should, in general, be small and easy to copy. If someone's
worried about how many times one gets copied they've probably got bigger
problems that they should be worrying about.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Feb 3 '07 #3

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

Similar topics

21
by: Roshan Naik | last post by:
typedef int foo ( foo ); // foo is a pointer-to-function type that takes another foo as argument and returns an int I need to achieve the above effect somehow. This is not accepted by any...
2
by: Gert Van den Eynde | last post by:
Hi all, What is "the best" way to have a callback from a C (or a Fortran) routine to a member function of a class? An example: I have objects of a class where double operator(double) is defined....
2
by: Gert Van den Eynde | last post by:
Hi all, I have a template class template<typename TC> class A{ public: ... protected: TC ObjTC; }
2
by: Faheem Mitha | last post by:
Hi, The following bit of code compiles fine with gcc 3.3 or later, but has problems with the Intel C++ compiler version 9.1, which produces the following error message. Is this a compiler...
6
by: Caleb | last post by:
I have a class that has two member functions of the same name: class MyClass { public: void someFunction(int arg); void someFunction(int arg, char blah); }; I'm trying to get a...
3
by: linarin | last post by:
#include <iostream> using namespace std; typedef bool (*CallableFunction)(int argc,char* argv); void DefineMyFunction(const char* name,CallableFunction func){ //here do the define action. }...
7
by: Protoman | last post by:
Is this an efficient way to integrate an explicit function of x from a to b: #include <iostream> using namespace std; class Integrate { public: Integrate(long double (*f)(long double&...
0
by: santiago.anderson | last post by:
About two years ago I came across somebody's public implementation of the tr1::function (and tr1::result_of) on the web. It was much more straightforward than the GNU or Boost version (it only...
5
by: S S | last post by:
Hi I have a requirement where I am declaring a map within a class. class abc { map <void*, void*mMap; // I do not pass compare struct here. .... }; Here I am not passing compare function,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.