5

I am trying to create smart pointer to my function.

I have the function:

double returnValue(int i){
       return i;
}

Its pretty simple to create raw pointer:

double (*somePTR)(int) = &returnValue;

But how to make smart pointer to the function, for example shared_ptr?

auto someptr = std::make_shared<double>(&returnValue);

I have tried a lot of of options, but nothing works.

6
  • Huh? Why do you want to do that? Commented Jan 18, 2018 at 14:43
  • 4
    Why would you want a smart pointer to a function? You can't create or destroy a function... Commented Jan 18, 2018 at 14:43
  • 1
    You need to understand first what smart pointers were created for. Commented Jan 18, 2018 at 14:45
  • why do you think you need this? Commented Jan 18, 2018 at 14:48
  • I wanted to make a few functions and then put the pointers of them to the vector and later use some functions while creating new objects. Commented Jan 18, 2018 at 15:00

5 Answers 5

11

I think you mean a smart function pointer, and for that there is since C++11, std::function, so how about this:

#include <functional>
#include <iostream>

double a(int i){
    return i;
}

double b(int i){
    return i * 2;
}

double c(double d, int i){
    return i - d;
}


int main() {
    std::function<double(int)> func = a;
    std::cout << func(42) << std::endl;//Output 42

    func = b;
    std::cout << func(42) << std::endl;//Output 84

    //func = c; //Error since c has a different signature (need one more argument), #type safety

    func = std::bind(c, 5 ,std::placeholders::_1); //func stores the needed 2nd argument
    std::cout << func(42) << std::endl;//Output 37

}
Sign up to request clarification or add additional context in comments.

2 Comments

Congrats on guessing what the OP intended!
Usually I flag or downvote such ambiguous questions, but since his/her intention was to improve code quality/safety and also that he/she was referring to function pointers, it was easy ;-)
5

You cannot. Smart pointers are only for object pointers not function pointers.

The intent behind smart pointers is to manage dynamically allocated memory. If a smart pointer is able to determine the dynamically allocated memory can be deallocated, it does that.

There is no such thing for function pointers.

Comments

3

Smart pointers exist to manage the data behind the address.

In case of a function pointer this data is program code. If the pointer runs out of scope it can't just delete the function.

Smart pointers can't hold function addresses because there wouldn't be any reason for them to do so.

Comments

0

You simply can not do that. Since you don't do memory management, like allocate dynamic memory or destroy a function when it goes out of scope, for example, it does not make sense to have a smart pointer for that. Have first a look to the intention of smart pointers and when would you use them.

Comments

0

Not sure about use-case, maybe it is needed only because of some uniformity in processing.

shared_ptr<> cannot contain a pointer to function, but can contain a dynamically allocated pointer to pointer to a function:

double f(int i) { return i; }
typedef double(*f_ptr)(int i);

int main (int _argc, char const **_argv) {
  std::shared_ptr<f_ptr> s(new f_ptr(f));
  (*s)(2);   // okay
  s.reset();   // releases a pointer to pointer, not function
  // ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.