1

before start, sorry for my poor English. Environment: c, c++, VS2019, after C99

Problem: I have to call functions which have named with prefix with callee name. I have many callee and variable prefix, so I want to handle it macro in preprocess-time for convenience with coding.

I tried:

`void prefix_function_name();
 void function_name(){
     (prefix_##__func__)();
 }
`

I know it can't be possible, cause func is const char[].

So I need to : get function name with a token not a string. or : turn a string into a token. ex)

void foo(); const char[] a = "foo"; 
a();

Is anyone have solution?


Here's my actual problem.

I have many functions with C code. And I'm going to make test code with gtest(C++). And I need to use mock(or stub. anyway) for functions. But, there is no way to call mock function instead of original function. So I need to make hook like this

`extern "C" void mock_foo();
 void foo(){
     return mock_foo(); // hook
     ... // actual code
 }
`

To avoid miss typing and human error, I wanted to make macro that makes hook for functions. Now I'm using macro like this. (Not in detail)

`#define MACRO(func_name) return mock_##func_name();
 void foo(){
     MACRO(foo) // hook
     ... // actual code
 }
`

And I wanted to get rid of typing function name.

Here's my actual problem. Is there any way?

7
  • 4
    what langauge is this? Please pick one. And this looks like a xy problem. Why do you need this? How is (prefix_##__func__)(); (assuming it would work) any better than prefix_function_name() ? Commented Jul 9, 2021 at 8:30
  • i mean inside function_name the name of the current function is always function_name no macros or runtime effects can change that Commented Jul 9, 2021 at 8:31
  • 4
    Why not just hardcode it? Explain your full scenario Commented Jul 9, 2021 at 8:39
  • 1
    i guess its one of the lessons one has to learn the hard way: Macros are not convenient for coding, rather the opposite. It took me a while to get convinced, because at first they look pretty cool Commented Jul 9, 2021 at 8:42
  • I second the above comments: you probably try to solve your problem a wrong way. Please describe the actual problem, only then an appropriate way to solve it can be chosen. Commented Jul 9, 2021 at 8:48

3 Answers 3

3

You can't do it the way you describe, since there is no way to turn a string (variable or literal) into a token. You might try a macro that defines your calling function, such as

#define DEFINE_FUNCTION(prefix, name)  void name () { prefix ##_ ##name ();}

and use it

void foo_func() {std::cout << "foo_func()\n";}     // assume <iostream> has been included

DEFINE_FUNCTION(foo, func)           // note no ; here

int main()
{
     func();       
}

This will work since DEFINE_FUNCTION(foo, func) has defined func() so it calls foo_func().

Generally I would strongly advise against doing the above. It may seem convenient to you now to use a macro, rather than defining your functions by hand. It will not seem so convenient in future when you, or someone else, is maintaining/extending the code - particularly if some of the functions needs to do things slightly differently than the macro sets up. Your code will quickly become a mess in that case.

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

1 Comment

Yes. Maybe I should use more manpower. I can use #define MACRO(prefix, funcname) instead of #define MACRO(prefix). I just wanted to use "__func__" instead of manpower.
0

maybe you want this:

void prefix_function_name();
#define INVOKER_FUNCTION(name) \
void name () {\
   prefix_##name();\
}

INVOKER_FUNCTION(function_name);

3 Comments

The OP wants to use a C-string as the function name, so this answer is probably not the case.
@M.Zhang - That's not what the OP asked for. The question is about how to get func() to call prefix_func(). The description in terms of a string literal is because the OP's first thought was to use the __func__ predefined macro (which gives a nul terminated string) to recover the name of the function as a token - and even the OP admits that is not possible.
Peter is right. I already have function and want to call other function in the function with the function name.
-1

I'm afraid, you have selected slightly-not-suitable language (i mean C++). But, You can do this (in C++) by using dll-s:

  • pack your functions into external library (.dll, .so).
  • then you can load library and export function by name and call it. See boost::dll:import_alias (for crossplatform code), LoadLibrary and GetProcAddress (for Windows).

1 Comment

Thanks for answering, but it would be too load. I just wanted to make macro for convenience. Repeatedly, thanks for answering. I would find this later.

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.