Page MenuHomePhabricator

How do I get the name of a function passed as a string?
Closed, ResolvedPublic

Asked by htgilroy on Feb 10 2015, 1:23 PM.


I'm trying to figure out how to get the name of a function passed as a string to a different function.

I need to have one function able to be passed the name of any future function passed as a string. That way the function can store the name of said function for later retrieval.


void foo();

void bar(string function_name)
       return function_name;


Updated 2,743 Days Ago

To answer the direct question, you can pass a function to another function directly, as an argument.

However, you cannot retrieve a function using a string with its name directly.

That said, there is also the question of "is this the best thing to do?" Because this method can create difficult-to-trace bugs, there are two things to consider before taking this route.

  1. Can the functions be combined somehow into one?
  2. Can we instead pass an object, with the function intended to be passed as a member function.

I am curious...where does this fit into your program overall? (Answer in comment.)

Updated 2,744 Days Ago

I feel like this function name could be passed in when the function was being created/being parsed. The user creates said function and as soon as the lexer/parser knows that it a function it could get the name of the function then.

New Answer


This question has been marked as closed, but you can still leave a new answer.