当不执行任何操作时是否可以使用虚拟左值?

发布于 2024-12-27 22:34:21 字数 534 浏览 5 评论 0原文

考虑以下 C99 函数:

void port_pin_set(const bool value, const uint8_t pin_mask)
{
    if (value) {
        PORT |= pin_mask;
    } else {
        PORT &= ~pin_mask;
    }
}

PORTdefine,例如:

#define PORT (P1OUT)

是否有办法重新定义 PORT,以便:

  • 它被接受为左值,
  • 该函数不执行任何操作。

我想要做的就是保持函数源不变,同时编译它而不执行任何操作。

编辑:我知道使用这样的左值可能不是最好的解决方案。我并不是在寻找这个特定问题的最佳解决方案,我对语言本身感兴趣。这是一个理论问题,而不是一个实用问题。

Consider the following C99 function:

void port_pin_set(const bool value, const uint8_t pin_mask)
{
    if (value) {
        PORT |= pin_mask;
    } else {
        PORT &= ~pin_mask;
    }
}

With PORT being a define, for example:

#define PORT (P1OUT)

Is there a way to redefine PORT so that:

  • it is accepted as an lvalue,
  • the function does not do anything.

What I want to do is to keep the function source as is, while compiling it to do nothing.

Edit: I am aware that using such an lvalue might not be the best solution. I am not looking for the best solution for this specific problem, I am interested in the language itself. This is a theoretical question, not a pragmatic one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

抽个烟儿 2025-01-03 22:34:21

C99 具有可用于此类任务的复合文字。它们的语法是强制转换后跟初始化程序:

#define PORT ((int){0})

应该在您的情况下执行此操作,只是您可能会收到一些有关未使用值等的警告。

任何像样的编译器都会优化对此类复合文字的分配。

C99 has compound literals that can be used for such a task. Their syntax is a cast followed by an initializer:

#define PORT ((int){0})

should do it in your case, only that you might get some warnings about unused values or so.

Any decent compiler will optimize assignments to such compound literal out.

超可爱的懒熊 2025-01-03 22:34:21

您可以定义与 P1OUT 类型相同的变量(例如 unsigned char),使其在标头中可用,并在源之一中定义它,例如this:

标头:

extern unsigned char dummy_P1OUT;
#define PORT (dummy_P1OUT)

C 文件:

unsigned char dummy_P1OUT;

用法:

PORT &= ~pin_mask;

if (PORT & pin_mask) {
    blink_led();
}

You can define a variable of the same type as your P1OUT (say, unsigned char), make it available in the header, and define it in one of the sources, like this:

Header:

extern unsigned char dummy_P1OUT;
#define PORT (dummy_P1OUT)

C file:

unsigned char dummy_P1OUT;

Usages:

PORT &= ~pin_mask;

if (PORT & pin_mask) {
    blink_led();
}
夜无邪 2025-01-03 22:34:21

这应该在 C99 中完成:

#ifdef PORT
#undef PORT
#endif

#define PORT int x = 0; x

对于你的情况。但我建议您将整个代码放在 #ifdef 之间,而不是重新定义 PORT

void port_pin_set(const bool value, const uint8_t pin_mask)
{
#ifdef BUILD_PORT_PIN_SET
    if (value) {
        PORT |= pin_mask;
    } else {
        PORT &= ~pin_mask;
    }
#endif
}

This should do it in C99:

#ifdef PORT
#undef PORT
#endif

#define PORT int x = 0; x

for your case. But I suggest you put the whole code between #ifdef's instead of redefining PORT:

void port_pin_set(const bool value, const uint8_t pin_mask)
{
#ifdef BUILD_PORT_PIN_SET
    if (value) {
        PORT |= pin_mask;
    } else {
        PORT &= ~pin_mask;
    }
#endif
}
场罚期间 2025-01-03 22:34:21

不是直接的左值,但我希望它能满足您的目的。 Try

#define PORT if (0) P1OUT

It 应该有效地使整个语句无效。您可能会收到一些警告,但恕我直言,这应该是可以克服的。

Not an lvalue directly, but I hope it will serve your purpose. Try

#define PORT if (0) P1OUT

It should effectively null the full statement. You may get some warnings, but that should be surmountable IMHO.

清浅ˋ旧时光 2025-01-03 22:34:21

C++ 中的一些变态可能可以达到这个目的。例如,您可以定义 PORT 以按值返回一个对象,该对象实现 |=&= 运算符。

在 CI 中可以通过以下方式找到一种方法:如果函数在开头定义一个整型变量,则可以将 PORT 定义为该变量或相关变量(P1OUT代码> 在你的情况下)。

PORT 定义为局部变量时 - 您可能希望编译器省略此代码:为局部变量分配一个未使用的值。

There are perversions in C++ that may do the trick. For instance, you may define PORT to return you an object by value, which implements the |= and &= operators.

In C I can figure-out a way the following way: define an integer variable at the beginning if the function, then PORT may by defined to either this variable or the relevant one (P1OUT in your case).

When PORT is defined to a local variable - you may hope that compiler will omit this code: assigning a local variable a value that is not used.

阿楠 2025-01-03 22:34:21

我在 ubuntu 上测试了下面的语句,它不会像在 Windows 上那样写入环境变量 PATH 的第一个字符。然而,根据定义,它确实会导致未定义的行为。

#define PORT (*(uint8_t*)(getenv("PATH")))

因此,为了确保安全,人们可以在程序的其他位置定义一个虚拟环境值,并继续设置它。

#include <stdlib.h>

#ifdef PORT
#undef PORT
#endif

#define SETDUMMYPORT (putenv("dummyPORT=x"))
#define PORT (*(uint8_t*)(getenv("dummyPORT")))

在程序中的某个位置设置虚拟环境变量:

int main(int argc, char* argv[])
{
    SETDUMMYPORT;
}

然后可以像您在问题中那样使用 PORT 。

I tested the statement below on ubuntu and it doesn't write to the first character of the environment variable PATH, where it does on Windows. However by definition it does cause undefined behavior.

#define PORT (*(uint8_t*)(getenv("PATH")))

So to make it safe one can define a dummy environment value elsewhere in the program and continue to set that instead.

#include <stdlib.h>

#ifdef PORT
#undef PORT
#endif

#define SETDUMMYPORT (putenv("dummyPORT=x"))
#define PORT (*(uint8_t*)(getenv("dummyPORT")))

Somewhere in the program set the dummy environment variable:

int main(int argc, char* argv[])
{
    SETDUMMYPORT;
}

Then PORT can be used as you have in your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文