C++ member function applied to object - c++

I want to call member function by passing it as template parameter, without using boost is possible. Here is an example off what I tried to do,
class object { void method(); }
{
object object_instance;
...
apply<object:: method>();
...
template<class F>
void apply() { F(object_instance); } // want to call object_instance.F()
}
that does not work, so question is how do I go about binding object method to an object.
Thanks
above is an example, not the real code. I have a bunch of functions differing only in name, but with many parameters, that I want to wrap around in operators.

Something like:
struct foo
{
void bar(void) {}
};
template <typename R, typename C>
R apply(C& pObject, R (C::*pFunc)())
{
return (pObject.*pFunc)();
}
int main(void)
{
foo f;
apply(f, &foo::bar);
}
this.

This is similar to your code, and will allow passing a member function as a template parameter:
class object { public: void method() {} };
object object_instance;
template<void (object::*F)()>
void apply() {
(object_instance.*F)();
}
int main() {
apply<&object::method>();
return 0;
}

As a member function isn't a type or integral value, you can't pass it as a template parameter. You could do what you seem to want by creating a struct which calls the member function, though having object_instance in scope in the template is rather smelly.
What are you actually trying to do?

This example is for rvalue references in c++ox. Does not work on all compilers.
class Sample
{
public:
void fun() { cout << "Sample::fun\n"; }
};
template<typename Function>
void FunCall( Function&& f)
{
f();
}
void RvaluesDemo()
{
FunCall([&]
{
Sample().fun();
});
}

Related

Implicitly pass the "this" OR the "type" of the calling class to a non-member template function

is there a way to implicitly pass the this of the calling class to a template function being called?
i tried the following so far:
template<class P, class T>
void funcA(P& p, T const* t = this){ //<--does not work.
//...some code here...
}
class TypeX { }
class TypeA {
public:
void DoSomething(){
TypeX x;
funcA(x, this); //compiles fine
//funcA(x); //<---my goal: implicitly pass "this" by default.
}
}
void main(){
TypeA objX;
objX.DoSomething();
}
for the type-of-"this" problem, i had the following working so far:
template<class T, class P>
constexpr auto funcA(P& p) {
//...some code here that also uses type T...
}
class TypeA {
public:
void DoSomething(){
TypeX x;
funcA<TypeA>(x); //<---explicitly passing TypeA. (compiles)
//funcA(x); //<---my goal.
//funcA<>(x); //<---my goal(fair enough)
}
}
}
as you can see, i explicitly pass the TypeA to funcA. looks like redundant to me. though it's currently running fine in my project, i tried making it cleaner by making funcA<TypeA>(x) to funcA(x). but no success so far. i'm hoping it's still possible to do this so i asked here in SO.
In a comment, you said,
funcA must be a non-member of a class, specially the target class it will be called.
Given that, it's not possible to use this as the default value of an argument of the function.
I don't see any problem with the following:
template<class P, class T>
void funcA(P& p, T const* t){
//...some code here...
}
class TypeX { };
class TypeA {
public:
void DoSomething(){
TypeX x;
funcA(x, this); // Explicitly pass "this".
}
};
It is not onerous either.

Is there a way to call varying function of member object based on template variable

I have the following code:
class Person {
void func1() {
obj_.function1();
}
void func2() {
obj_.function2();
}
void func3() {
obj_.function3();
}
Object obj_;
};
It's pretty obvious there is a pattern here...
I would like to know if I can call different functions based on template argument.
the functions inside Object are regular member functions.
Is there any way to do something like the following?
class Person {
template <typename Func>
void generic() {
obj_.Func();
}
Object obj_;
};
Another question: Is there a type for function?
I believe it's the key point for it's doable or not.
Thanks
template <auto Func>
void generic() {
(obj_.*Func)();
}
Then foo.generic<&Object::function1>() calls function1.
Prior to c++17:
template <void (Object::*Func)()>
void generic() {
(obj_.*Func)();
}
should work.

Declare new function inside a class using a template

I'm still learning about templates. I'm not sure whether you can declare/(automatically define) a function inside a class (method) using a template. That is, I have a function template defined like this, for example:
template<typename T>
T getT() {
T result;
return result;
}
And a class, where I want the "new function" to be created, based on the template, like this:
class World{
public:
World();
~World();
getT<int>; //"Magically" create new function from the template (return type 'int')
}
What I actually want is to have only a method with the specific given type in World. That means when I want to "magically" create a method based on the template, I want to sort of copy-paste the template function into the class but with the given type.
For example:
class World{
public:
World();
~World();
//The magically created function with T equal to int
int getT(){
int result;
return result;
}
}
Then of course I expect to be able to call the function:
int main(){
World world; //Create world object
world.getT<int>; //Call the function
return 0;
}
Even though here I say I would call it with getT<int>, it could be only getT() (if it is a perfect copy-paste of the template function).
Be carfull
template<typename T>
T& getT() {
T result;
return result;
}
Will return a reference to a temporary. Please do
template<typename T>
T getT() {
T result;
return result;
}
without "&"
And if it's just for get a specific member, you can use std::tuple.
https://en.cppreference.com/w/cpp/utility/tuple/get
getT<int>; //"Magically" create new function from the template (return type 'int')
I don't think that will work.
It appears as though you would like to be able to use templates like macro expansion. Unfortunately, they are very different things and templates don't work like macro expansion.
However, you can use something like the following:
template<typename T>
struct GetHelper
{
T get()
{
return T{};
}
};
class World : private GetHelper<int>,
private GetHelper<double>
{
public:
World() {}
~World() {}
template <typename T>
get()
{
return static_cast<GetHelper<T>*>(this)->get();
}
};
Now you can use:
World w;
int a = w.get<int>();
double b = w.get<double>();
You may also hide GetHelper as a private type of World as:
class World
{
private:
template<typename T>
struct GetHelper
{
T get()
{
return T{};
}
};
struct Data : GetHelper<int>,
GetHelper<double>{};
Data data;
public:
World() {}
~World() {}
template <typename T>
get()
{
return static_cast<GetHelper<T>*>(&data)->get();
}
};

Error using boost::bind

Lets say I have an array of functions :
name::Function _actions[10];
The name::Function is just a wrapper for std::function:
class Function{
public :
std::function<void(float dt)> Function;
void exec(float dt){
Function(dt);
}
};
And now I have a function to make it generic so it can bind any classes functions :
template<class T>
void bindFunction(int action,void (T::*function)(float) , T* classPtr)
{
_actions[action] = boost::bind(&function, this, boost::placeholders::_1);
}
And want to use it inside a class that inherits this method (the base class is generic but I think it shouldn´t be a problem)
DefaultMovableEntity()
{
bindFunction(1, &DefaultMovableEntity::func1, this);
}
void func1(float dt)
{
//Code
}
However I keep getting erros :
What is the main problem? I thought im doing everything right. Thanks.
Edit1
After removing the & from the function and replacing this got following error:
There are several problems with your code:
You can't assign your bind result to the array since your array member type is Function, not std::function.
You need to remove & as said before in comments.
Your member named Function is same as class name so compiler may think you are trying to call class constructor.
The following code should work:
class Function {
public:
std::function<void(float)> func;
void exec(float dt) {
func(dt);
}
};
and bindFunction code:
template<class T>
void bindFunction(int action, void (T::*function)(float), T* classPtr)
{
_actions[action].func = boost::bind(function, classPtr, boost::placeholders::_1);
}
However I would suggest slight improvement of Function class so that it doesn't expose its members directly:
class Function
{
private:
std::function<void(float)> func;
public:
Function() = default;
Function(std::function<void(float)> f) : func(f) { }
void exec(float dt) {
func(dt);
}
};
And corresponding change of your bindFunction:
template<class T>
void bindFunction(int action, void (T::*function)(float), T* classPtr)
{
_actions[action] = Function(boost::bind(function, classPtr, boost::placeholders::_1));
}

Template and function pointer confuse

I started learning c++ now. Im quite confuse about this definition. This is just a throwaway code as the actual implementation was on the book I was reading
class A
{
public:
template<class T>
void Hello(void(T::*func)())
{
func(); // Not working. Error term does not evaluate to function taking 0 argument
}
};
class B
{
public:
void funcA()
{
std::cout << "Hello world" << std::endl;
}
// This is called function pointers
void funcB(void(*ptr)())
{
ptr();
}
};
void main()
{
A a;
a.Hello(&B::funcA);
}
First is that what sort of template is that? If it's a template class shouldn't I delcare the template at the top of the class A?
Also why can't I call the func on Hello() like the same as calling a function pointer?
First is that what sort of template is that? If it's a template class shouldn't I delcare the template at the top of the class A?
A::Hello is member function template.
Also why can't I call the func on Hello() like the same as calling a function pointer?
Because the parameter func of Hello is not a function pointer, but a member function pointer. You need an object to call on it. e.g.
template<class T>
void Hello(void(T::*func)())
{
T t;
(t.*func)();
}
LIVE