0

给定这样一个带有用户提供的 struct的 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被删除,将不再有警告。

避免警告的最佳方法是什么?

  1. 我是否应该在每个 gperf 文件中定义 bar::LookupTableElement(使用该结构的 gperf 不止一个)?
  2. 或者使用类似的东西(在 GCC 手册中没有找到关闭它的开关)?
  3. 取消注释// namespace bar {// } // end namespace bar更改struct bar::LookupTableElementstruct LookupTableElement. 但是通过这种方式,我们会将很多东西拖到命名空间中(看看生成的 foo.cc 你就知道了)。
  4. 还有什么想法吗?
4

1 回答 1

1

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)。

于 2014-04-02T08:24:35.500 回答