Pass Both Functions and Member-Function to Template Method - c++

I've been looking into this issue for a while, I hope to get some clarification here.
I have developed a Template class. One of its methods takes for input two functions with different parameters, as follows:
template<typename S, typename T, typename R>
void TGraph<S,T,R>::BFS(const S &key, bool(*relValid)(R), void(*manip)(string, string, R)) {
//Being here R,S members of the template class TGraph, do stuff including
if(relValid("some R") ){
manip("some S", "another S", "someR");
}
}
This works like a charm when passing standard functions in the main.cpp.
int main(){
TGraph2<string, User, Relation> graph;
graph.BFS("Rube", alwaysValid, printRelation);
}
void printRelation(string id1, string id2, Relation rel){
cout<<id1<<" ha una relazione con "<<id2<<" di tipo "<<rel.type<<endl;
}
bool alwaysValid(Relation rel){
return true;
}
I cannot compile when using the template class in another class, and using member-functions instead.
class DataSet {
private:
TGraph<string, User, Relation> _users;
....
}
I found out this is because of the difference between pointer-to-function and pointer-to-member-function types.
A solutions is to make static the members, which is not possible in my case (I need to access some non-static members).``
Now I wonder, does exist some "clean" solution to allow BFS method to work with any function (member or not)?
Or at least to make it work with member-functions (which actually is my need).

Member functions and non-member functions are called entirely different. As a result, they don't mix well. The approach to address this issue is to accept function objects instead of either function pointers or pointer to member functions is to accept function objects. These still won't necessarily accept member functions right away but carefully implementing the resulting function can deal transparently with all kinds of function objects.
To deal with function objects there are broadly two different approaches:
Accept the function object as template parameter. Doing so is great for performance: the compilers happily inline all of the operations if they can see through the call chain (and passing lambda functions instead of function pointers makes that rather successful). The downside of this approach is that a function template needs to be exposed. Depending on the context that may be rather problematic. Also, the required signature isn't visible in the function signature and needs to specified elsewhere.
You could write your BFS() member as below. The use of std::invoke() deal with passing passing anything which can be considered a function object including member function pointers and member pointers:
template<typename S, typename T, typename R, typename RelValid, typename Manip>
void TGraph<S,T,R>::BFS(const S &key, RelValid relValid, Manip manip) {
//Being here R,S members of the template class TGraph, do stuff including
if(std::invoke(relValid, "some R") ){
std::invoke(manip, "some S", "another S", "someR");
}
}
Accept a type-erased function object which only specifies the function's interface. std::function<RC(T...)> is the obvious first choice for a type-erased function object. Function pointers, member function pointers, and function objects in general readily convert to compatible std::function<RC(T...)> types.
Using std::function<...> to replace function pointer is straight forward: the function pointers already use the signature which is needed as template argument for std::function<...>:
template<typename S, typename T, typename R>
void TGraph<S,T,R>::BFS(const S &key, std::function<bool(R)> relValid, std::function<void(string, string, R)> manip) {
//Being here R,S members of the template class TGraph, do stuff including
if(relValid("some R") ){
manip("some S", "another S", "someR");
}
}
Which approach to choose depends somewhat on the expected use. If the function object is used heavily, e.g., it is called once for each node visited, I'd prefer using a template argument. Where the function is used just a few times or it needs to show up in a non-templatised interface I'd use std::function<...> (or similar).

Related

Is it possible for C++ to implement function pointers point to different parameter lists?

I recently wrote about the function of class member function callbacks. I need to save the callback object and function pointer, then call the function pointer and fill in the appropriate parameters where the callback is needed.
I started out as a form of typedef void (AAA::*Function)(int a, int b);, but when I need to support different parameter lists of member function, I obviously need a dynamic way to implement it.
class AAA
{
public:
int add(int a, int b)
{
return (a + b);
}
};
class BBB
{
public:
void setValue(std::string value)
{
this->value = value;
}
private:
std::string value;
};
class CCC
{
public:
void bind(??? p) // Binding objects and callback functions.
{
this->p = p;
}
template <class... Args>
auto callback(Args&&... args) // Autofill parameter list.
{
return this->p(std::forward<Args>(args)...);
}
private:
??? p; // How is this function pointer implemented?
};
int main()
{
AAA aaa;
BBB bbb;
CCC ccc;
ccc.bind(???(aaa, &AAA::add));
int number = ccc.callback(5, 6);
ccc.bind(???(bbb, &BBB::setValue));
ccc.callback("Hello");
system("pause");
return 0;
}
I don't know how can I implement the function pointer "???".
You basically are asking to have fully dynamicly typed and checked function calls.
To have fully dynamic function calls, you basically have to throw out the C++ function call system.
This is a bad idea, but I'll tell you how to do it.
A dynamicly callable object looks roughly like this:
using dynamic_function = std::function< std::any( std::vector<std::any> ) >
where use use
struct nothing_t {};
when we want to return void.
Then you write machinery that takes an object and a specific signature, and wraps it up.
template<class R, class...Args, class F>
struct dynamic_function_maker {
template<std::size_t...Is>
dynamic_function operator()(std::index_sequence<Is...>, F&& f)const {
return [f=std::forward<F>(f)](std::vector<std::any> args)->std::any {
if (sizeof...(Is) != args.size())
throw std::invalid_argument("Wrong number of arguments");
if constexpr( std::is_same< std::invoke_result_t<F const&, Args... >, void >{} )
{
f( std::any_cast<Args>(args[Is])... );
return nothing_t{};
}
else
{
return f( std::any_cast<Args>(args[Is])... );
}
};
}
dynamic_function operator()(F&& f)const {
return (*this)(std::make_index_sequence<sizeof...(Args)>{}, std::forward<F>(f));
}
};
template<class R, class...Args, class F>
dynamic_function make_dynamic_function(F f){
return dynamic_function_maker<R,Args...,F>{}(std::forward<F>(f));
}
next you'll want to deduce signatures of function pointers and the like:
template<class R, class...Args>
dynamic_function make_dynamic_function(R(*f)(Args...)){
return dynamic_function_maker<R,Args...,F>{}(std::forward<F>(f));
}
template<class Tclass R, class...Args>
dynamic_function make_dynamic_function(T* t, R(T::*f)(Args...)){
return dynamic_function_maker<R,Args...,F>{}(
[t,f](auto&&...args)->decltype(auto){return (t->*f)(decltype(args)(args)...);}
);
}
then after fixing typos above you should be able to solve your original problem.
Again, as someone who can actually write and understand the above code, I strongly advise you not to use it. It is fragile and dangerous.
There is almost never a good reason to store callbacks in places where you don't know what the arguments you are going to call it with.
There should be a different type and instance of CCC for each set of arguments you want to call it with. 99/100 times when people ask this question, they are asking the wrong question.
C++ is a type-safe language. This means that you cannot do exactly what you've outlined in your question. A pointer to a function that takes specific parameters is a different type from a pointer to a function that takes different parameters. This is fundamental to C++.
std::bind can be use to type-erase different types to the same type, but you get a single type at the end, that can be called only with a matching set of parameters (if any). It is not possible to invoke the "underlying" bound function, with its real parameters. That's because the whole purpose of std::bind is to make them disappear, and inaccessible. That's what std::bind is for.
You only have a limited set options to make this work while staying with the bounds and constraints of C++'s type-safety.
Make use of a void *, in some fashion. Actually, don't. Don't do that. That will just cause more problems, and headache.
Have a separate list and classes of callbacks, one list for each set of callbacks that take a specific set of parameters. You must know, at the point of invoking a callback, what parameters you intend to pass. So, just get your callback from the appropriate list.
Make use of std::variant. The type-safe std::variant is C++17 only (but boost has a similar template that's mostly equivalent, and available with older C++ revisions). All your callbacks take a single std::variant parameter, a variant of every possible set of parameters (designated as a std::tuple of them, or some class/struct instance). Each callback will have to decide what to do if it receives a std::variant containing the wrong parameter value.
Alternatively, the std::variant can be a variant of different std::function types, thus shifting the responsibility of type-checking to the caller, instead of each callback.
The bottom line is that C++ is fundamentally a type-safe language; and this is precisely one of the reasons why one would choose to use C++ instead of a different language that does not have the same kind of type-safety.
But being a type-safe language, that means that you have certain limitations when it comes to juggling different types together. Specifically: you can't. Everything in C++ is always, and must be, a single type.

How can you make a function that takes as a parameter an arbitrary function pointer?

I am trying to do the following, and I am not even sure if it's possible.
I want a function that takes in an arbitrary function pointer to pass it to a different function (I am aware this is a code smell, good software engineering practices are not what I want to discuss right now).
In other words what I am looking for would look like:
void method1(arbitraty pointer p)
{
method2(p);
}
I am not sure if there is a way to declare arbitrary function pointers (the return value is guaranteed to be void but the parameters are arbitrary, both in number and type)
Use template and use SFINAE to enable it only if the type deduced is a function pointer type:
template <typename T, std::enable_if_t<std::is_function<T>::value, int> = 0>
void method1(T* p)
{
// ...
}
This might be considered over-engineering by some, but you could try the following:
Create an enum of each callback that you are interested in:
enum GlfwCallback {
KeyCallback,
FramebufferSizeCallback,
// etc.
};
Then create a type family that associates each of these with the corresponding function pointer type. Do this by creating a template struct and repeatedly specializing it:
template<GflwCallback callback>
struct GlfwCallbackType {};
template<>
struct GlfwCallbackType<KeyCallback> {
using CallbackType = GLFWkeyfun;
// or
using CallbackType = void(*)(GLFWwindow *, int, int, int, int);
};
template<>
struct GlfwCallbackType<FramebufferSizeCallback> {
using CallbackType = GLFWframebuffersizefun;
};
// etc.
Then you can write
template<GlfwCallback callback>
void method1(GlfwCallbackType<callback>::CallbackType p) {
// do something with p
method2<callback>(p);
};
Also, note that you can add other types and even static functions and data members to your "type family" as needed by your application.
A possibility to do what you want, but in a nice, type-safe fashion, would be the use of functors, i.e. classes that define an overloading for the operator().
Since a functor is a class, you could set the arguments as data memebers, and move the implementation/calling of the functions that you want to pass as arbitrary pointers into the operator() method, where you can have access to all the parameters through the this pointer.
Furthermore, you can define a hierarchy of functors, each one with specialized parameters and implementations, so you can modify the signature of method2 like the following:
method2(BaseFunctor* myfunctor) {
if (myfunctor)
(*myfucntor)();
}
and setup the right type of functor object in the calling context.
Also check out lambdas (c++11), that are basically a shortcut to functors definition.

lambda as template parameter with access to 'this' pointer?

I have to create objects of three-four classes, all inherited from one base class, but some of the objects need to have different behavior - like complete change of one function; I can do this through more inheritance and polymorphism, but it doesn't seem like a good idea.
My first solution was to use specialized templates(for every nonstandard case), but then I have though about lambdas as template parameter(like here: Can we use a lambda-expression as the default value for a function argument? ) and use them instead class method(like here: C++11 lambda and template specialization ) - for me it's much better solution, because I only have to pass lambda for every weird situation:
auto default_lambda = [&]() -> int {return this->Sth;};
template<typename functor = decltype(default_lambda)>
class SomeClass{
...
Problem is with this pointer - method which I want to change need access to non-static methods and lambda is defined outside of non-static method. Moreover, I can't pass reference to class to lambda, because it's a template class(or maybe I'm wrong?).
Of course, I can use specialized template or just function pointers, but I really like solution with lambdas and I consider it much more fine than my other ideas.
Is there any way to "avoid" this problem? Or maybe my idea was bad all along?
There are at least three obvious problems with your approach:
The class SomeClass won't get access to private members, i.e. use of this is out of question.
You attempt to bind this from the context but there is no context i.e. nothing bind to. You will have to pass the object to dealt with a function parameter.
You only specified a type of the function object but no instance, i.e. you won't be able to use it later.
That said, it seems you could just use the type of a custom function object type rather than some lambda function (sure, this is absolutely unsexy but in return it actually works):
struct default_lambda {
template <typename T>
int operator()(T const& o) const { return o.x(); }
};
template <typename F = default_lambda>
class SomeClass {
...
};
If you need complete change of one function, you have two choices:
One virtual function, using perhaps local classes + type erasure if you have many such objects and you don't want to create many namespace scope types:
std::function, which can be rebound later if you wish.
Example code for the first solution (you can even make this a template):
std::unique_ptr<my_base2> get_an_object()
{
class impl : public my_base2
{
void my_virtual_function() { blah blah; }
};
return std::unique_ptr<my_base2>(new impl);
}
Both are generally superior to templates in this situation (but without context it is hard to tell).

function to iterate over members and call function passed as argument

I have a std::vector<T> of some type that's part of a class and that I need to iterate through in a lot of different places in my code, so I thought I'd be smart and create a function IterateAttributes, and pass it a boost::function object that I can in the loop and pass a single element and then I can pass any function to do work on the elements.
This seems a good idea until you have to implement it, then the problem comes of what does the passed in function return and does it need other arguments. It seems like I either have to find a way to do this more generically, like using templates, or I have to create overloads with function objects taking different args.
I think the first (more generic) options is probably better, however how would I go about that?
Below is a trial that doesn't work, however if I wanted to have a number of args, and all but the Attribute (a struct) arg mandatory. How should I go about it?
template <typename T> template <typename arg>
void ElementNode::IterateAttributes(boost::function<T (arg, Attribute)> func_)
{
std::vector<Attribute>::iterator it = v_attributes.begin();
for (; it != v_attributes.end(); it++)
{
func_(arg, *it);
}
}
Is that what you mean:
template <typename T, typename arg>
void ElementNode::IterateAttributes(boost::function<T (arg, Attribute)> func_, arg a)
{
std::vector<Attribute>::iterator it = v_attributes.begin();
for (; it != v_attributes.end(); it++)
{
func_(a, *it);
}
}
that allows only one parameter of any type - if you want you can introduce also version for more parameters.
About return value - what to do about it depends on what value it acctually is - the generic (and probably unnecesary) solution would be to return std::list<T>, but that would create more problems than it would solve i guess. If return type varies (not only in type but also in meaning) then I suggest modyfying templated function so it takes reference/pointer to overall result and updates it accordingly:
template <typename T> template <typename arg>
void ElementNode::IterateAttributes(boost::function<voidT (arg, Attribute, T&)> func_)
{
std::vector<Attribute>::iterator it = v_attributes.begin();
T result;
for (; it != v_attributes.end(); it++)
{
func_(arg, *it, result);
}
return result;
}
That's a quick workaround, it works but it's ugly, error prone, and pain to debug.
If you want variable parameter amount, then you would have to create several templates of above function - i just tested if it's possible:
template <typename T>
T boo(T){
}
template <typename T, typename TT>
TT boo(T,TT){
}
void test()
{
int i;
i= boo<int>(0);
i=boo<int,double>(0,0.0);
}
You must remember that functions passed to IterateAttributes must match exatly parameters given to Iterate function. That also means that you cannot use in it's prototype default values - probably you will have to define several overloaded versions like
void func_(Attribute,arg1, arg2,arg3){...}
void func_(Attribute A,arg1 a1,arg2 a2){func_(A,a1, a2,default3);}
void func_(Attribute A,arg1 a1){func_(A,a1, default2,default3);}
void func_(Attribute A){func_(A,default1, default2,default3);}
a) You want to iterate over the array and do something with each element there: in this case, you want functions that all take an array element and return void. Simple.
b) You want to partially apply functions with more arguments on each element: Write a custom functor around your function which stores the additional, pre-assigned arguments, or use boost::bind to effectively do the same.
Example:
vector<string> myStrings; // assign some values
// define a function with an additional argument
void myFunc(string message, string value)
{
cout << message << value << endl;
}
// allow partial application, i.e. "currying"
struct local_function
{
static string message;
static void myFunc_Curried(string value)
{
myFunc(message, value);
}
};
local_function::message = "the array contains: ";
// apply the curried function on all elements in the array
for_each(myStrings.begin(), myStrings.end(), local_function::myFunc_Curried);
The functor operates statically only for demonstration purposes. If message is bound to an instance of the struct, you will need something like boost::bind anyway to bind the instance pointer this in order to actually call the curried function. However, if the function I want to apply is used only locally, I prefer following the more readable static approach.
What you are trying to accomplish makes very good sense, and is also built directly into functional languages (for example F#). It is possible to achieve in C++, but requires some workarounds in the aforementioned case b. Please note if writing your own functor, as in my example, that it is common to place the arguments you want to curry away always at the beginning, and to "fill in" the arguments from the beginning to the end when partially applying.
Summarizing the comments and more thoughts:
Use bind to bind the other arguments, then use for_each on the resulting functor.
To handle return values, you need to think about what the return values mean. If you need to use the values in some way (say, perform a reduction, or use them to influence whether or not to continue performing the operation, etc), then you can use another functor to wrap the original to perform the thing you want.
You could do the same or more using BOOST_FOREACH or C++0x for each. That would even take less code to write.

What is the use of member template functions in c++?

Given a class with a member template function like this one:
template <typename t>
class complex
{
public:
complex(t r, t im);
template<typename u>
complex(const complex<u>&);
private:
//what would be the type of real and imaginary data members here.
}
I am confused about member template functions, please provide an example by which the need of member template functions becomes clear to me.
Also, tell me the use of member template functions in c++, what are the situations where we use member template functions?
It gives you the ability to do conversions:
complex<int> ci;
complex<float> cf(ci);
So, if you have two types T1 and T2 and you can assign a T1 to a T2, this will make it possible to assign a complex<T1> to a complex<T2>.
As for the question in your code (what would be the type of real and imaginary data members here):
template <typename t>
class complex
{
...
private:
t real_part;
t imaginary_part;
};
The most common valuable use for member template functions I come across in my day-to-day is to reduce code complexity by providing one templated function instead of many functions that do essentially the same thing.
For example, suppose you are working on a server that receives half a dozen related messages and saves the incoming data to half a dozen tables in a database. A straightforward implementation would be to implement 6 message handling functions (psudocode):
class MessageProcessor
{
void OnMessage(const char* msg);
void ProcessMessage100(Data100* data);
void ProcessMessage101(Data101* data);
void ProcessMessage102(Data102* data);
void ProcessMessage103(Data103* data);
void ProcessMessage104(Data104* data);
void ProcessMessage105(Data105* data);
};
MessageProcessor::OnMessage(const char* msg)
{
unsigned int * msgType = ((unsigned int*)msg);
switch( *msgType )
{
case 100 :
ProcessMessage100((Data100*),sg);
break;
case 101 :
ProcessMessage101((Data101*),sg);
break;
::
}
}
MessageProcessor::ProcessMessage100(Data100* data)
{
Record100* record = GetRecord100(key);
record->SetValue(xyz);
}
MessageProcessor::ProcessMessage101(Data101* data)
{
Record101* record = GetRecord101(key);
record->SetValue(xyz);
}
: :
There is an opportunity here to generalize the ProcessMessage() functions, since they do essentially the same thing:
class MessageProcessor
{
OnMessage(const char* msg);
template<class Record, class Data> void Process(Data* data);
};
template<class Record, class Data>
void MessageProcessor::Process<Record,Data>(Data* data)
{
Record* record = GetRecord(key);
record->SetValue(xyz);
}
The GetRecord function can also be generalized, yielding a codebase with 2 functions where there used to be 12. This improves the code by virtue of it being simpler with fewer moving parts, simpler to understand and maintain.
The general purpose and functionality of member function templates is in no way different from that of ordinary (non-member) function templates. The only [irrelevant] difference is that member functions have access to the implicit this parameter.
You understand the general purpose of ordinary function templates, do you? Well, in that case you should understand the general purpose of member function templates, because it is exactly the same.
Using the example you provided, the member template function allows you to construct an instance of complex<T> from complex<U>.
As a concrete example of when this might be useful, suppose you had a complex<double> but wanted a complex<float>. Without the constructor the types are unrelated so the regular copy constructor wouldn't work.
Often you would like some member function of your class to operate on a range. By having templated member functions you make it possible to operate on ranges independent of the container that supplies the range without providing an free function.
The same goes for Functors. Often you'd write a functor that operates on some sipecial pair of iterators but soon realize that it is possible to have the Functor operate on any kind of range. So instead of supplying the template parameters through the encapsulating struct you can supply them through the member function operator() and make type deduction possible.
The first examples that come to mind:
In some container constructors (or assign methods) to take input iterators of unknown type.
std::complex to allow operating on different types than the one the std::complex was instantiated from.
In shared_ptr (whether std::tr1:: or boost::) so that you can keep different types of pointers into a shared object instance in the heap (for which the pointer types can be obtained).
In thread (whether std:: in c++0x or boost::) to receive a functor of unknown type that will be called by the thread instance.
In all cases the usage is the same: you have a function that operates on types that are unknown. As AndreyT perfectly states the same that with regular functions.