【问题标题】:What is this macro?这个宏是什么?
【发布时间】:2016-04-11 13:01:23
【问题描述】:

ruby.h中,有很多这样定义的函数宏:

static inline int
    #if defined(HAVE_PROTOTYPES)
    rb_type(VALUE obj)
    #else
    rb_type(obj)
       VALUE obj;
    #endif
    {
        if (FIXNUM_P(obj)) return T_FIXNUM;
        if (obj == Qnil) return T_NIL;
        if (obj == Qfalse) return T_FALSE;
        if (obj == Qtrue) return T_TRUE;
        if (obj == Qundef) return T_UNDEF;
        if (SYMBOL_P(obj)) return T_SYMBOL;
        return BUILTIN_TYPE(obj);
    }

如果HAVE_PROTOTYPES==1,根据我的理解,这个函数会是这样的:

static inline int rb_type(VALUE obj)
{
   ...
}

然而,如果HAVE_PROPOTYPES==0,函数定义是这样的:

static inline int rb_type(VALUE obj)
      VALUE obj;
{
    ...
}

我不明白这在语法上是否正确。应该怎么理解?

【问题讨论】:

  • 顺便说一下,应该是static inline int rb_type(obj) 而不是(VALUE obj)
  • HAVE_PROTOTYPES 是 0 还是 1,仍然是定义的。
  • 是K&R声明风格
  • 请相应地标记 Ruby 1.8 问题。

标签: c ruby ruby-1.8


【解决方案1】:
static inline int rb_type(VALUE obj)
      VALUE obj;    # what the hack is this?
{
    ...
}

这是 K&R C。没有人再使用它了。它已被弃用至少 20 年。

很久以前的函数定义是这样写的:

int myfunc(myparam)
  int myparam;
{
   ...
}

而不是

int myfunc(int myparam)
{
   ...
}

所以HAVE_PROTOTYPES 将始终在任何体面的编译器上定义。

【讨论】:

    猜你喜欢
    • 2015-01-09
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多