给定这样一个带有用户提供的结构的gperf文件:
%define class-name Table
%define lookup-function-name m
%struct-type
%language=C++
%{
#include <cstring>
#include <cstdio>
// defination of LookupTableElement is put to a header file in the real project and included in
namespace bar {
struct LookupTableElement {
const char *name;
void (*func) ();
};
}
// a handler
void ack() {
puts("you said hello");
}
// namespace bar {
%}
struct bar::LookupTableElement;//gperf needs the declaration
%%
######
hello, ack
######
%%
// } // end namespace bar
int main() {
auto p = Table::m("hello", sizeof("hello") - 1);
if (!p) return 1;
p->func();
return 0;
}汇编:
$ gperf foo.gperf > foo.cc && g++ -std=c++0x foo.cc让g++ (gcc 4.7.3和4.8.2测试)警告:
foo.gperf:26:13: warning: declaration ‘struct bar::LookupTableElement’ does not declare anything [enabled by default]
struct bar::LookupTableElement;//declare look up table's element
^如果namespace bar被删除,将不再有任何警告。
避免警告的最好方法是什么?
// namespace bar {和// } // end namespace bar,并将struct bar::LookupTableElement更改为struct LookupTableElement。但是以这种方式,我们将把很多东西拖到名称空间中(看看生成的foo.cc,您就知道了)。发布于 2014-04-02 08:24:35
gperf有一种选择:
-T, --omit-struct-type
Prevents the transfer of the type declaration to the output file. Use
this option if the type is already defined elsewhere.所以,没有任何命名空间技巧,
struct bar::LookupTableElement;这个选项会生成完全可以接受的代码(例如gperf -T foo.gperf > foo.gperf.cpp)。
https://stackoverflow.com/questions/22322100
复制相似问题