看了一个C源代码,这个C的lib广泛引用在包括firefox等很多地方
看到如下的代码,百思不解
static int PTRCALL
PREFIX(scanComment)(const ENCODING *enc, const char *ptr,
const char *end, const char **nextTokPtr)
{
if (ptr != end) {
if (!CHAR_MATCHES(enc, ptr, ASCII_MINUS)) {
*nextTokPtr = ptr;
return XML_TOK_INVALID;
}
PREFIX(scanComment)(const ENCODING *enc, const char *ptr,
const char *end, const char **nextTokPtr)
{
if (ptr != end) {
if (!CHAR_MATCHES(enc, ptr, ASCII_MINUS)) {
*nextTokPtr = ptr;
return XML_TOK_INVALID;
}
这函数定义,比较奇怪,突破我的知识范围了。我反复查了源代码,搞了一个多小时。
所有的一切都是宏定义
#ifndef PTRCALL
#define PTRCALL
#endif
#ifndef PREFIX
#define PREFIX(ident) ident
#endif
#define PTRCALL
#endif
#ifndef PREFIX
#define PREFIX(ident) ident
#endif
PREFIX(scanComment) =scanComment
static int PTRCALL PREFIX(scanComment)(const ENCODING *enc, const char *ptr, const char *end, const char **nextTokPtr)
其实就是
static int PTRCALL scanComment (const ENCODING *enc, const char *ptr, const char *end, const char **nextTokPtr)
scanComment 是函数名。这宏定义不是变态吗?有啥好出?
自己也写了个,看看这玩意
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#ifndef PTRCALL
#define PTRCALL
#endif
#ifndef PREFIX
#define PREFIX(ident) ident
#endif
using namespace std;
typedef struct prefix {
const char *name;
char *binding;
} PREFIX;
typedef int (PTRCALL *SCANNER)(int a);
static int PTRCALL PREFIX(scanComment)(int a)
{
cout<<"ss";
}
int main()
{
PREFIX(scanComment)(2);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#ifndef PTRCALL
#define PTRCALL
#endif
#ifndef PREFIX
#define PREFIX(ident) ident
#endif
using namespace std;
typedef struct prefix {
const char *name;
char *binding;
} PREFIX;
typedef int (PTRCALL *SCANNER)(int a);
static int PTRCALL PREFIX(scanComment)(int a)
{
cout<<"ss";
}
int main()
{
PREFIX(scanComment)(2);
return 0;
}