如何根据条件简洁地分配给结构的成员?
How can I concisely assign to the members of a struct depending on a condition?
我有一些代码如下所示:
struct mystruct
{
/* lots of members */
};
void mystruct_init( struct mystruct* dst, int const condition )
{
if ( condition )
{
/* initialize members individually a certain way */
}
else
{
/* initialize members individually another way */
}
}
我正在考虑的选项:
- 最简单的方法是拥有一个分配给每个成员并调用它的函数。我应该只是希望编译器优化那个调用吗?
- 定义一个宏来显式避免函数调用开销。
- 把一切都写得很长。
在 C11 中处理这种情况的正确方法是什么?
只需编写一个初始化成员的函数,或者如果需要(基于意见),使用 MACRO。
顺便说一句,我个人会这样做:
void mystruct_init( struct mystruct* dst, int const condition )
{
if ( condition )
init_first_way(..);
else
init_second_way(..);
}
或者只使用三元运算符。请记住,您关心 可读性 并始终牢记:
Simplicity is a virtue!
我真的认为在这个阶段担心优化会成为不成熟优化的受害者,因为我怀疑它会成为瓶颈。
一般来说,如果你想优化你的代码,分析你的代码(当它运行时带有优化标志,很多人不知道这一点,我就是其中之一:),找到瓶颈并尝试优化该瓶颈。
我认为这里没有明确的规定。对我来说,这取决于作者的口味。
两种明显的方式是:
// initialize members that are independent of 'condition'
if (condition) {
// initialize members one way
}
else {
// initialize members another way
}
同样可以写成:
// initialize members that are independent of 'condition'
// initialize members based on 'condition'
dst->memberx = condition ? something : something_else;
// ...
请不要担心一个函数调用的开销。
我同意已经发布的答案(@gsamaras 和@Arun)。我只是想展示另一种我发现好几次有用的方法。
方法是用两个(或多个)相关的初始化值做一些常量,然后根据一个(或多个)条件进行简单的赋值。
简单示例:
#include<stdio.h>
#include <string.h>
struct mystruct
{
int a;
float b;
};
const struct mystruct initializer_a = { 1, 3.4 };
const struct mystruct initializer_b = { 5, 7.2 };
int main (void)
{
int condition = 0;
struct mystruct ms = condition ? initializer_a : initializer_b;
printf("%d %f\n", ms.a, ms.b);
return 1;
}
我有一些代码如下所示:
struct mystruct
{
/* lots of members */
};
void mystruct_init( struct mystruct* dst, int const condition )
{
if ( condition )
{
/* initialize members individually a certain way */
}
else
{
/* initialize members individually another way */
}
}
我正在考虑的选项:
- 最简单的方法是拥有一个分配给每个成员并调用它的函数。我应该只是希望编译器优化那个调用吗?
- 定义一个宏来显式避免函数调用开销。
- 把一切都写得很长。
在 C11 中处理这种情况的正确方法是什么?
只需编写一个初始化成员的函数,或者如果需要(基于意见),使用 MACRO。
顺便说一句,我个人会这样做:
void mystruct_init( struct mystruct* dst, int const condition )
{
if ( condition )
init_first_way(..);
else
init_second_way(..);
}
或者只使用三元运算符。请记住,您关心 可读性 并始终牢记:
Simplicity is a virtue!
我真的认为在这个阶段担心优化会成为不成熟优化的受害者,因为我怀疑它会成为瓶颈。
一般来说,如果你想优化你的代码,分析你的代码(当它运行时带有优化标志,很多人不知道这一点,我就是其中之一:
我认为这里没有明确的规定。对我来说,这取决于作者的口味。
两种明显的方式是:
// initialize members that are independent of 'condition'
if (condition) {
// initialize members one way
}
else {
// initialize members another way
}
同样可以写成:
// initialize members that are independent of 'condition'
// initialize members based on 'condition'
dst->memberx = condition ? something : something_else;
// ...
请不要担心一个函数调用的开销。
我同意已经发布的答案(@gsamaras 和@Arun)。我只是想展示另一种我发现好几次有用的方法。
方法是用两个(或多个)相关的初始化值做一些常量,然后根据一个(或多个)条件进行简单的赋值。
简单示例:
#include<stdio.h>
#include <string.h>
struct mystruct
{
int a;
float b;
};
const struct mystruct initializer_a = { 1, 3.4 };
const struct mystruct initializer_b = { 5, 7.2 };
int main (void)
{
int condition = 0;
struct mystruct ms = condition ? initializer_a : initializer_b;
printf("%d %f\n", ms.a, ms.b);
return 1;
}