Reputation: 2423
I'm trying to use mem_fun_ref to send a reference to a member function of an object to another function, but I get error C2064: term does not evaluate to a function taking 0 arguments.
I didn't reflect this in the example, but I need to send the mem_fun_ref_t to a virtual function, which is why I didn't just make Flip a function template that takes a simple function object.
#include <iostream>
#include <string>
#include <functional>
class Coin
{
public:
Coin() {}
std::string Flip ()
{
srand(23);
int side = rand() % 2 + 1;
std::string result = "";
if (side == 1)
result = "heads.";
else
result = "tails.";
return result;
}
};
std::string Flip(std::mem_fun_ref_t<std::string, Coin> flip)
{
return flip();
}
int main()
{
std::cout << "Flipping a coin..." << std::endl;
std::string output = Flip(std::mem_fun_ref<std::string, Coin>(&Coin::Flip));
std::cout << "The coin came up " << output << std::endl;
return 0;
}
Upvotes: 2
Views: 1166
Reputation: 29229
You should read up on static member functions, as well as member function pointers. There are three ways you can fix your problem.
First is to make Coin::Flip
a static member function:
#include <string>
#include <iostream>
typedef std::string (*Flipper)(); // Function pointer typedef
class Coin
{
public:
Coin() {}
// Static member function. A pointer to a static member function can be
// held in a regular function pointer.
static std::string Flip ()
{
srand(23);
int side = rand() % 2 + 1;
return (side == 1) ? "heads." : "tails.";
}
};
std::string Flip(Flipper flipper)
{
return flipper();
}
int main()
{
std::cout << "Flipping a coin..." << std::endl;
std::string output = Flip(&Coin::Flip);
std::cout << "The coin came up " << output << std::endl;
return 0;
}
If Coin::Flip
needs to be a non-static member function, you can pass a Coin
instance to Flip
, along with the member function pointer:
#include <functional>
#include <string>
#include <iostream>
class Coin
{
public:
Coin() {}
// Non-static member function.
std::string Flip ()
{
srand(23);
int side = rand() % 2 + 1;
return (side == 1) ? "heads." : "tails.";
}
};
typedef std::mem_fun_ref_t<std::string, Coin> Flipper;
// We need the Coin instance as well as the member function pointer.
std::string Flip(Coin& coin, Flipper flipper)
{
// Invoke the flipper member function on the coin instance
return flipper(coin);
}
int main()
{
// Since we're using a non-static member function, we need an instance
// of Coin.
Coin coin;
std::cout << "Flipping a coin..." << std::endl;
std::string output = Flip(coin, mem_fun_ref(&Coin::Flip));
std::cout << "The coin came up " << output << std::endl;
return 0;
}
Finally, if the Flipper
functor can be a member function from any kind of object (not only Coin), and you don't want the Flip
free function to be a template, you'll need std::function
and std::bind
that are part of the recent C++11 standard. std::function
is a general-purpose polymorphic function wrapper that works with any kind of callable target: free functions, member functions, function objects, etc. If you can't use C++11, the Boost library has the equivalents boost::function
and boost::bind
.
#include <functional>
#include <string>
#include <iostream>
class Coin
{
public:
Coin() {}
// Non-static member function.
std::string Flip ()
{
srand(23);
int side = rand() % 2 + 1;
return (side == 1) ? "heads." : "tails.";
}
// Static member function.
static std::string StaticFlip()
{
srand(23);
int side = rand() % 2 + 1;
return (side == 1) ? "heads." : "tails.";
}
};
// Flipper is a generic function object wrapper that works with free functions,
// function objects, static member functions, and non-static member functions.
typedef std::function<std::string ()> Flipper;
std::string Flip(Flipper flipper)
{
return flipper();
}
int main()
{
// Example with non-static member function
Coin coin;
// Bind a Coin instance along with a Coin::Flip member function pointer.
Flipper flipper1 = std::bind(&Coin::Flip, &coin);
std::cout << "Flipping a coin..." << std::endl;
std::string output = Flip(flipper1);
std::cout << "The coin came up " << output << std::endl;
// Example with static member function
Flipper flipper2 = &Coin::StaticFlip;
std::cout << "Flipping a coin..." << std::endl;
output = Flip(flipper2);
std::cout << "The coin came up " << output << std::endl;
return 0;
}
Upvotes: 3