How can I obtain the identifier of the current function? - c++

In C/C++, we have the __FUNCTION__ macro which is replaced with a string, holding the name of the current function. But what if I want the function's identifier? That is, not a string, but something I could use as a token to create other identifiers, e.g., if we have
#define MAGIC /* ... */
#define MORE_MAGIC MAGIC ## _bar
void foo() {
printf("%s\n",__FUNCTION__);
MORE_MAGIC();
}
void foo_bar() {
printf("%s\n",__FUNCTION__);
}
void baz() {
printf("%s\n",__FUNCTION__);
MORE_MAGIC();
}
void baz_bar() {
printf("%s\n",__FUNCTION__);
}
int main() {
foo();
}
should print
foo
foo_bar
baz
baz_bar
Notes:
I'm interested in preprocessing-time only.
I would rather not replace my function definitions with a preprocessor call - although I know that would probably work.

Unfortunately you can't. Because you can not unstringify a macro[1].
In other words, you can not remove quotes around the string that generated by __FUNCTION__ and contact it by _bar.

If it's compile-time you want, and for a simple case like your, it might be possible with preprocessor macros and the concatenation operator ##. Maybe something like
#define MORE_MAGIC(f) f##_bar
...
void foo_bar()
{
}
void foo()
{
MORE_MAGIC(foo)();
}
It's not possible to get the name foo automatically though, it has to be explicitly named in the macro "call".

Related

Alternatives to stringifying the variable name in C++11

In my code, I have repeatedly this expression:
T foo;
do_sth(foo, "foo");
I am considering stringifying the variable name, like this:
#define VARNAME(Var) (#Var)
void do_sth_new(T foo) { do_sth(foo, VARNAME(foo)); };
T foo;
do_sth_new(foo);
Is it good practice? Is there any better alternative in C++11?
As you show it, it doesn't work since VARNAME(foo) will always be "foo" (as this is the parameter's name). You have to write do_sth_new itself as macro:
#define do_sth_new(_foo) \
do { do_sth(_foo, #_foo); } while (false)
Only then will this:
T bar;
do_sth_new(bar);
generate "bar".
And no, there is no alternative to using the preprocessor since this is an operation on the lexical level. You'd need LISP-level modification of the AST in the language to have a better solution, which is unlikely to ever happen.
Sadly, no. There is still no solution (not even in C++17) to this problem. There might be something once static reflection will be added to C++. But for the time being you're stuck with the macro.
There is no real way to avoid the macro to do the stringify.
What you can do is dress it in a more c++ object-oriented way, especially if you want to do multiple different methods that take an object and its var name, and if this is a debug feature you might want to disable in production.
So I'm proposing you declare a template class DebugContextWrap, and objects of this type (or const ref) can be passed into the function as a single parameter, instead of having 2 parameters.
The one downside is that where your function code actually wants to access the actual value then you would have to perform an indirection through operator -> or data() as you do for iterators.
You could then write a macro that generates instances of DebugContextWrap - something like:
template class FooType
class DebugContextWrap
{
FooType& fooVal;
const char* debugName;
const char* debug__FILE__val;
const int debug__LINE__val;
public:
DebugContextWrap(FooType& fooVal,
const char* debugName, const char* debug__FILE__val, const int debug__LINE__val)
{ ... }
DebugContextWrap(FooType& fooVal) // implicit when caller doesn't use DEBUG_WRAP
{ ... }
FooType* operator ->()const
{ return &foo; }
FooType& operator *()const
{ return foo; }
FooType& Data()const
{ return foo; }
const char* DebugName()const
{ return debugName; }
};
#define DEBUG_WRAP(foo) \
DebugContextWrap<decltype foo>(foo, #foo, __FILE__, __LINE__)
void do_sth(const DebugContextWrap<FooType>& foo);
do_sth(DEBUG_WRAP(foovar));

How to force a function to be compiled in C++?

I was just experimenting with C++. I was trying to write a small macro so that all the functions that I define are automatically stored in a map so that I can query, at run time, what functions exist and run them too. The code is as follows:
#include <map>
using namespace std;
typedef void (*funcPointer)();
map <char*, funcPointer> funcList;
#define Function(x) void x() { funcList[#x] = x;
#define End }
I was used funcPointer and End only for easy readability and implementation. Now, I can define a function as
Function(helloWorld)
cout << "Hello World";
End
Now, to read the function names as a list and run all the functions, I use the following code:
int main() {
//helloWorld();
for (map<char*, funcPointer>::iterator I = funcList.begin(); I != funcList.end(); I++) {
printf(I->first);
I->second();
}
getchar();
return 0;
}
The problem is, if I keep the first line of main() (helloWorld();) commented, the compiler doesn't compile the function and skips it for optimization, as according to the compiler, it is never used. So, the function list turns up empty. If, instead, I call the function once, every thing works perfectly, except that it prints "Hello World" twice. Also, I wrote the macro specifically so I do not have to do that.
So, is there any way that I can force the compiler to compile a function even if it is not used?
The problem is that the code to register the function is inside the function, so won't happen unless you call the function. You might instead register it by initialising a global variable, which will happen automatically before main begins. This might look something like
struct funcRegistration {
funcRegistration(char * name, funcPointer func) {funcList[name] = func;}
};
#define Function(x) \
void x(); \
funcRegistration x##_registration(#x, x); \
void x() {
The compiler compiles the function, however your map won't be populated unless its called.
Because funcList[#x] = x; comes inside the function block { } after macro expansion.

How to define macro function with variable name

I want to define a macro like
#define AUTO_FUC(x) void x(){}
to define a function named x.
I think the function name will be replaced by x passed by arg x.
AUTO_FUC("test")//it should equal to void test(){} I think
But it didn't.
So how to do this in C++?
I use MinGW and use the macro twice with different args.
AUTO_FUC("A")
AUTO_FUC("B")
It didn't give me function A() and B().Compiler told me void someClass::x() cannot be overloaded.So I know the macro didn't work.
Thanks ,I realize the void "A"() is not a correct function. I forgot I pass a macro as arg and the marco is a string.
And further question. What I really want is create a function with name according to a variable value.And variable value can be write like string a = a;? It should have " am I right? So If I use a string like "A", how can I use the A to create the function void A() when pass the value to marco?
#include <string>
#include <iostream>
#define AUTO_FUC(x) \
void x() \
{\
std::cout<<"hi";\
}\
int main()
{
std::string A = "A";
AUTO_FUC(A); //it's equal to AUTO_FUC("A") it wrong
A(); //So it's undefined;
}
The problem is that you're not passing in A and B (identifiers). You're passing in "A" and "B" (string literals). Macros are textual replacement. So they expand to:
void "A"(){}
void "B"(){}
Surely you'll agree that's not correct C++ code. Change it to this:
AUTO_FUC(A)
AUTO_FUC(B)
[ UPDATE: you've edited your question and now show code where the macro parameters are quoted. Next time, try to post your actual code when asking the question! ;-P ]
Your code works just fine... see it running at ideone.com here
#include <iostream>
#define AUTO_FUC(x) void x(){ std::cout << "hi!\n"; }
AUTO_FUC(test)
int main()
{
test();
}
stdout
hi!

Unusual C++ function declaration

There are a few legal ways which can we declare a function in C++.
Some of the legal ways are:
void function ();
void function (void);
dataType function (dataType);
and so on...
Recently, I came across a function declaration as such:
void (function) (); //Take note of the braces around the function name
I have never seen somehting like this before, when I tested it in a C++ compiler, it runs without any warning or compilation errors.
My question is: Why is void (function) (); a legal way to decalre a function prototype? Is there any special meaning to declare a function in this way? Or does it just work normally like any other function declaration?
One difference is that enclosing it in parenthesis prevents the function-like macros expansion by the preprocessor. As mentioned in the other answers it makes no difference though to the actual compiler.
For instance:
// somewhere buried deep in a header
#define function(a, b) a + b
// your code
void function() { // this expands the macro and gives compilation error
}
void (function)() { // this does not expand and works as expected
}
This comes in handy for instance when the bright minds behind the Microsoft Visual Studio library decided to provide function-like macros for things like min and max. (There are other ways like #undef to go around this).
Note that object-like macros (e.g. #define function 3 + 4) are still expanded.
The preprocessor is just a dumb text replacement tool (as opposed to the compiler which is just a (smart) text replacement tool). It takes the macro definition and replaces it everywhere. He is not aware of the semantics of what he replaces.
For instance:
// somewhere buried deep in a header
#define function 3 + 2
// your code
void function() {
}
The preprocessor sees the word function and textually replaces it with the string 3 + 2. He is unaware that function is a id-name part of a function declaration and definition. After the preprocess phase there come the actual compile phases. So the compiler actually sees:
// your code
void 3 + 2() {
}
which does not make any sense to him and gives an error.
For function-like macros
// somewhere buried deep in a header
#define function(a, b) a + b
The preprocessor does the same except that it expects two ‘tokens’ enclosed in parenthesis separated by comma (the parameters) and does the replacement. (again no semantics aware):
int d = function(2, 3);
//will be replaced by the preprocessor to:
int d = 2 + 3; // passes compilation phase
void function();
// the preprocessor doesn’t find the arguments for function so it gives an error.
However if it encounters (function) it will not try to expand it (it ignores it). It is just a rule.
it's the same as
void function();
you can declare it as
void ((function)) ();
if you want :)
be careful not to mix this up with the function pointer declaration syntax.
There is nothing special about it, it means exactly the same as the version without parentheses. It is just an artifact of how the syntax is declared. Usually you see the use of parentheses around the function name when a function pointer is declared, e.g.
void (*function_pointer)() = nullptr;
// a function pointer to a function taking and returning void
in contrast to
void *function();
// a function declaration of a function taking void and returning void*
I think it works the same as a normal function because function pointers are declared like: void (*function)() so if you leave out the * then it should be just a function.
It corresponds to the C++ grammar. If to simplify then one of the rules for defining of the declarator looks as
declarator:
(declarator)
So you can write for example
void (function) ();
or
void ( (function) () );
or even the following way
struct A
{
void ( ( function )() const );
};
I think you may find that was:
void (*function) ();
since there is no benefit to using void (function)(); or void (((((function)))))(); for that matter, they're equivalent. If I'm mistaken and it's not a typo, the answer is that you can put as many parentheses around the function name as you like, subject to compiler limitations, as per the code for output6() below.
If I'm not mistaken, that one with the * actually declares a function pointer which can be used to hold a pointer to a function. It does not declare a function at all, just a pointer that can be used to reference a function.
Like an int pointer (for example), the function pointer can point to an arbitrary function, parameters notwhithstanding.
So for example:
#include <iostream>
void (((((output6)))))() { std::cout << 6; }
void output7() { std::cout << 7; }
void output8() { std::cout << 8; }
void (*fn)();
int main() {
fn = &output6; fn();
fn = &output7; fn();
fn = &output8; fn();
std::cout << '\n';
}
would output 678.

How Curly Brackets work in Boost::extension, how to make such macros on my own?

I look at how we use Boost::Extension BOOST_EXTENSION_TYPE_MAP_FUNCTION macro.
For example like this:
BOOST_EXTENSION_TYPE_MAP_FUNCTION
{
std::map<std::string, boost::extensions::factory<service> > &factories(types.get());
factories["file_service"].set<file_service>();
}
BOOST_EXTENSION_TYPE_MAP_FUNCTION macro is defined in extension.hpp.
I wonder how this macro understands what is in Curly Brackets and how for example expand this macro to something that would cout anything like "Hello extended macro"?
Let me put my comment into an answer...
A macro is an instruction to the compiler (I use the collective term here) to substitute at that location the symbols defined as that macro, for example
#define FOO 1
int val = FOO; // at this point, FOO is replaced with 1
(p.s. please don't do this in C++)
Now, what is happening in your case is that there is a set of symbols (the signature of a function) defined as a macro, so all that happens is the compiler will substitute the macro with the symbols, and the end result would look (roughly) like this:
void boost_extension_exported_type_map_function(boost::extensions::type_map& types)
{
std::map<std::string, boost::extensions::factory<service> > &factories(types.get());
factories["file_service"].set<file_service>();
}
Which as you can see is a simple function. You can do this too (but don't unless you have a very good reason)
#define BOB void foo(std::string const& bar)
BOB
{
std::cout << "HEllo: " << bar << std::endl;
}
It simply allows a user to define their own implementation for that function... presumably somewhere else - it takes the address of that function and uses it via a pointer...