当不执行任何操作时是否可以使用虚拟左值?
考虑以下 C99 函数:
void port_pin_set(const bool value, const uint8_t pin_mask)
{
if (value) {
PORT |= pin_mask;
} else {
PORT &= ~pin_mask;
}
}
以 PORT
为 define
,例如:
#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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
C99 具有可用于此类任务的复合文字。它们的语法是强制转换后跟初始化程序:
应该在您的情况下执行此操作,只是您可能会收到一些有关未使用值等的警告。
任何像样的编译器都会优化对此类复合文字的分配。
C99 has compound literals that can be used for such a task. Their syntax is a cast followed by an initializer:
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.
您可以定义与
P1OUT
类型相同的变量(例如unsigned char
),使其在标头中可用,并在源之一中定义它,例如this:标头:
C 文件:
用法:
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:
C file:
Usages:
这应该在 C99 中完成:
对于你的情况。但我建议您将整个代码放在
#ifdef
之间,而不是重新定义PORT
:This should do it in C99:
for your case. But I suggest you put the whole code between
#ifdef
's instead of redefiningPORT
:不是直接的左值,但我希望它能满足您的目的。 Try
It 应该有效地使整个语句无效。您可能会收到一些警告,但恕我直言,这应该是可以克服的。
Not an
lvalue
directly, but I hope it will serve your purpose. TryIt should effectively null the full statement. You may get some warnings, but that should be surmountable IMHO.
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.我在 ubuntu 上测试了下面的语句,它不会像在 Windows 上那样写入环境变量 PATH 的第一个字符。然而,根据定义,它确实会导致未定义的行为。
因此,为了确保安全,人们可以在程序的其他位置定义一个虚拟环境值,并继续设置它。
在程序中的某个位置设置虚拟环境变量:
然后可以像您在问题中那样使用 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.
So to make it safe one can define a dummy environment value elsewhere in the program and continue to set that instead.
Somewhere in the program set the dummy environment variable:
Then PORT can be used as you have in your question.