至少在我这里编译运行起来是没有问题的(g++ 4.8.1),但是在gdb调试的时候有如下状况。或者$在gdb中有什么含义吗?
代码
#include<cstdio>
using namespace std;
int $(int x) { return x; }
int f(int x) { return x; }
int main()
{
return 0;
}
gdb
Breakpoint 1, main () at test.cpp:9
9 return 0;
(gdb) p f(4)
$1 = 4
(gdb) p $(4)
Program received signal SIGSEGV, Segmentation fault.
0x00000004 in ?? ()
The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on".
Evaluation of the expression containing the function
(at 0x0x4) will be abandoned.
When the function is done executing, GDB will silently stop.
(gdb)
The meaning of this command is explained as follows:
$ represents the last historical value, which is 4, which is the result of the function call f(4), which is equivalent to:
The first 4 is used as the function address, and the second one is used as the function parameter, which is equivalent to:
Such a function call is obviously illegal.
After reading the gdb document and testing it, this is where gdb and g++ have not negotiated well.
Since C and C++ do not allow variable names to contain $, gdb uses it as a keyword. As a result, gdb's print call and other commands cannot call functions named $. The break command is OK. If gdb is not modified, this problem may not be solved.
Look at the results of these commands to understand it better: