需要帮助将C宏重写为函数

时间:2022-03-16 02:03:22

I need some help rewriting the following line in a more type safe way and rewriting it as a function, however the fact that this code is defined inside a function is making it hard for me to think of a clever way of doing it because apparently it would involve declaring several arguments.

我需要一些帮助重写以下行更类型安全的方式并将其改写为一个函数,但是这段代码是在一个函数中定义的让我很难想到一个聪明的方法显然因为它需要声明几个参数。

#define CHECK(id) if(table->cells[id]) isgood[table->cells[id]-1] = 0;

where table is a struct and isgood is an int.

表是一个结构,而isgood是一个整数。

6 个解决方案

#1


5  

Straight translation (if table->cells[id] is int):

直译(如表->细胞[id]为int):

void check(int id, int*isgood) { if (id) isgood[id-1] = 0; }

Call with:

电话:

check(table->cells[id], isgood);

However, I'd rename/rework this a bit. I'd especially change the name. There is also no error checking - ie, if table->cells[id]==0, you're going to try to set isgood[-1], which would be bad.

但是,我要重命名/重做一下。我特别想改个名字。也没有错误检查—例如,如果表->单元格[id]= 0,您将尝试设置isgood[-1],这将是糟糕的。

#2


4  

apparently it would involve declaring several arguments

显然,这需要声明几个论点

What is wrong with that?

这有什么不对吗?

#3


2  

Why not just a function that receives table and id and does this?

为什么不只是一个接收表和id的函数呢?

void foo(TableType & t, int id)
{
    if (t.cells[id]) 
        isgood[t.cells[id]-1] = 0;
}

p.s.

注。

It's a bad macro indeed. The name is very misleading.

这确实是一个糟糕的宏。这个名字很容易引起误解。

p.p.s.

最大功率。

The whole thing is rather weird and the logic of this function escapes me. What exactly is this supposed to achieve?

整件事很奇怪,这个函数的逻辑让我无法理解。这究竟应该达到什么目的?

#4


2  

If you're working in C++, I'd consider making check a member function of the table, which seems like a good candidate for a class:

如果你在c++工作,我会考虑检查表的一个成员函数,这似乎是一个很好的类的候选者:

class Table {
    //...
    public bool check(int id) {
        if (this->cells[id]) {
            this->isGood[id] = 0;
            // the line you have, isgood[table->cells[id]-1] = 0 looks buggy:
            // you treat table->cells[id] as a true/false value one line ago;
            // probably not a valid array index? I'm taking a stab at what to do.
        }
    }
}

#5


1  

It is generally a good idea not to refer to variables in a macro.

通常最好不要在宏中引用变量。

First, make sure the name is meaningful. what are you checking for? And is there a side effect?

首先,确保名字是有意义的。你在检查什么?有副作用吗?

void update_valid_cells(Table& table, int id, BoolArray& validArray)
{
     if(table.cells[id]==NULL) return;
     validArray[id]-1=false;
}

#6


1  

I think C99 can qualify functions as inline so you get the speedup of no-function-call without using macros. Also, most C compilers support extensions such as __inline for this purpose.

我认为C99可以将函数限定为内联,这样就可以在不使用宏的情况下获得无功能调用的加速。此外,大多数C编译器都支持这样的扩展,如__inline。

#1


5  

Straight translation (if table->cells[id] is int):

直译(如表->细胞[id]为int):

void check(int id, int*isgood) { if (id) isgood[id-1] = 0; }

Call with:

电话:

check(table->cells[id], isgood);

However, I'd rename/rework this a bit. I'd especially change the name. There is also no error checking - ie, if table->cells[id]==0, you're going to try to set isgood[-1], which would be bad.

但是,我要重命名/重做一下。我特别想改个名字。也没有错误检查—例如,如果表->单元格[id]= 0,您将尝试设置isgood[-1],这将是糟糕的。

#2


4  

apparently it would involve declaring several arguments

显然,这需要声明几个论点

What is wrong with that?

这有什么不对吗?

#3


2  

Why not just a function that receives table and id and does this?

为什么不只是一个接收表和id的函数呢?

void foo(TableType & t, int id)
{
    if (t.cells[id]) 
        isgood[t.cells[id]-1] = 0;
}

p.s.

注。

It's a bad macro indeed. The name is very misleading.

这确实是一个糟糕的宏。这个名字很容易引起误解。

p.p.s.

最大功率。

The whole thing is rather weird and the logic of this function escapes me. What exactly is this supposed to achieve?

整件事很奇怪,这个函数的逻辑让我无法理解。这究竟应该达到什么目的?

#4


2  

If you're working in C++, I'd consider making check a member function of the table, which seems like a good candidate for a class:

如果你在c++工作,我会考虑检查表的一个成员函数,这似乎是一个很好的类的候选者:

class Table {
    //...
    public bool check(int id) {
        if (this->cells[id]) {
            this->isGood[id] = 0;
            // the line you have, isgood[table->cells[id]-1] = 0 looks buggy:
            // you treat table->cells[id] as a true/false value one line ago;
            // probably not a valid array index? I'm taking a stab at what to do.
        }
    }
}

#5


1  

It is generally a good idea not to refer to variables in a macro.

通常最好不要在宏中引用变量。

First, make sure the name is meaningful. what are you checking for? And is there a side effect?

首先,确保名字是有意义的。你在检查什么?有副作用吗?

void update_valid_cells(Table& table, int id, BoolArray& validArray)
{
     if(table.cells[id]==NULL) return;
     validArray[id]-1=false;
}

#6


1  

I think C99 can qualify functions as inline so you get the speedup of no-function-call without using macros. Also, most C compilers support extensions such as __inline for this purpose.

我认为C99可以将函数限定为内联,这样就可以在不使用宏的情况下获得无功能调用的加速。此外,大多数C编译器都支持这样的扩展,如__inline。