what is the advantage or disadvantage between these function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef TIMECOUNT_H
#define TIMECOUNT_H

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

void timeCount(std::tr1::function<void()> fc, std::string const& NAME = "")
{
  std::clock_t begin, end;
  begin = std::clock();
  fc();
  end = std::clock();
  std::cout<<"time of the "<<NAME<<" : summation = "<<(end - begin) / CLOCKS_PER_SEC<<"\n";
}

template<typename zeroArg>
void timeCount(zeroArg fc, std::string const& NAME = "")
{
  std::clock_t begin, end;
  begin = std::clock();
  fc();
  end = std::clock();
  std::cout<<"time of the "<<NAME<<" : summation = "<<(end - begin) / CLOCKS_PER_SEC<<"\n";
}

#endif 


Thanks a lot
The first one makes it clear that you want a function, and the second one also works with functors.
Thanks, do I have any way to make the "function" take arbitrary arguments?
Last edited on
I would think both would accept function objects unless tr1::function<> is not the exact same thing as boost::bind (which I thought it was). Though I'd take the tr1::function by const reference instead of by value.

The first one allows more flexibility than the second because arguments can be bound via tr1::bind (ie, the actual signature of the called function can literally be anything as long as the tr1::bind() call is right). The second one mandates that the function take zero parameters.
Thanks again, how could I pass the it by const&?
 
void timeCount(std::tr1::function<void()> fc, std::string const& NAME = "")

this wouldn't work

besides, how could I pass arbitrary parameters to the
std::tr1::function<void()> fc?

I try something like this
1
2
3
4
5
6
7
8
9
10
11
void hohoho(int a, int b)
{

}

void testTimeCount()
{  

  std::tr1::function<void(int, int)> i = std::tr1::bind(hohoho, 44, 88);
  timeCount( i() ); //this wouldn't work
}

error: no match for call to '(std::tr1::function<void(int, int)>) ()
Last edited on
1
2
3
4
void timeCount( std::tr1::function<void()> const& fc, std::string const& name = std::string() )
{
    // ... etc
}
void timeCount( std::tr1::function<void()> const& fc, std::string const& name = std::string() )

Thanks, it did work, I tried it before but it can't compile, maybe I made another fault

The other question is how could I bind arbitrary arguments to the
 
std::tr1::function<void()>

?
Thanks a lot
Last edited on
1
2
3
4
5
int add4( int a, int b, int c, int d )
    { return a + b + c + d; }

std::tr1::function<int(int,int)> add2 = std::tr1::bind( &add4, _1, _2, 0, 0 );
add2( 4, 7 );   // 11 


(if it works the same as boost::bind.)
Thanks, after your tutorial, I did it
1
2
3
4
std::tr1::function<void()> nothing = std::tr1::bind( &add4, 4, 7, 0, 0 );
timeCount(nothing); //should like this
timeCount(std::tr1::bind( &add4, 4, 7, 0, 0 )); //or like this
timeCount(nothing());//this one would cause error message 


Thanks a lot
Last edited on
Topic archived. No new replies allowed.