这是内联函数的有效用法吗?
Is this valid usage of inline functions?
假设我有这段代码(不要介意 SecondsToMinutes 和 MinutesToHours 是彼此的副本)
inline float SecondsToMinutes(float seconds)
{
return seconds / 60.0;
}
inline float MinutesToHours(float minutes)
{
return minutes / 60.0;
}
inline float HoursToDays(float minutes)
{
return minutes / 24.0;
}
inline float SeconndsToHours(float seconds)
{
return MinutesToHours(SecondsToMinutes(seconds));
}
inline float MinutesToDays(float minutes)
{
return HoursToDays(MinutesToHours(minutes));
}
inline float SeconndsDays(float seconds)
{
return MinutesToDays(SecondsToMinutes(seconds));
}
这是内联的有效用法吗?是否有意义?这是好习惯吗?毕竟,如果我没记错的话,inline是指函数调用被函数体代替了,所以
return MinutesToDays(SecondsToMinutes(seconds))
应该等同于
return seconds / 60.0 / 60.0 / 24.0
对吗?
还是只使用宏更好?
#define EXCHANGE_SEC_MIN(x) (x / 60.0)
#define EXCHANGE_MIN_H(x) (x / 60.0)
#define EXCHANGE_H_D(x) (x / 24.0)
#define EXCHANGE_SEC_H(x) (EXCHANGE_MIN_H(EXCHANGE_SEC_MIN(x)))
#define EXCHANGE_MIN_D(x) (EXCHANGE_H_D(EXCHANGE_MIN_H(x)))
#define EXCHANGE_SEC_D(x) (EXCHANGE_MIN_D(EXCHANGE_SEC_MIN(x)))
哪种做法更好?或者两者都不是?我希望其他人对此有美分。
Is this valid usage of inline? Does it make sense? Is this good
practice? After all, if I recall correctly, inline means that function
calls are replaced by function bodies, so
当然是。你让它更清晰,这总是好的。
return seconds / 60.0 / 60.0 / 24.0
是的,结果就是这样。或者应该。 inline
只是一个提示,编译可能会做出其他决定。但是对于这样一个liner,编译器会内联它。
宏?为什么?函数都能搞定,为什么要用宏?
inline
是不是意思是函数调用被函数体代替了。至少这并不意味着在过去的 fifteen-something 年里:优化器现在已经远远超出了接受开发人员的命令,并且无论您是否指定 inline
.
都会执行内联
inline
其实就是"this function may be defined multiple times, and the linker should sort it out and keep at most a single definition at the end. I'm responsible for ensuring that all definitions are identical".
如果你真的,真的想要自己强制执行内联(函数的 body 在调用者内部的实际插入),你将不得不使用 compiler-specific 扩展名,例如 __attribute__((always_inline))
.
当函数在 header 中定义时,您通常需要 inline
,因为这个 header 最终将包含在多个翻译单元中,因此定义将被重复。因此,假设您的代码在 header 中,这是 inline
.
的一个很好的用法
假设您的内联函数的定义在使用时对编译器可见(例如,它们位于每个编译单元需要的 #include
d 头文件中),那么您的用法是有效。
然而,inline
只是对编译器的提示。标准允许编译器忽略该提示,而不是内联该函数。编译器不内联函数的标准高度依赖于编译器。
宏是另一种选择,但还有其他问题....包括它们不尊重程序范围,并且很容易编写宏 - 有意或无意 - 导致与人们预期不同的行为.所以内联函数通常被认为是更可取的。
Is this valid usage of inline? Does it make sense?
嗯,是的,但不是。
此时它不会造成任何伤害,但也不会像您认为的那样。
在 excellent post about inline
deft_code 中正确地说:
It is said that inline
hints to the compiler that you think the
function should be inlined. That may have been true in 1998, but a
decade later the compiler needs no such hints. Not to mention humans
are usually wrong when it comes to optimizing code, so most compilers
flat out ignore the 'hint'.
因此,如果您这样做不会伤害任何人,但您的编译器听取您的提示的机会几乎为 0。如果它认为适合内联代码,它会自己这样做。
inline
现在主要用于链接器,因为它允许在多个编译单元中进行多个定义。
如果你想确保你的代码尽可能快并且你可以访问 C++11,你应该使用 constexpr
:
constexpr float SecondsToMinutes(float seconds)
{
return seconds / 60.0;
}
//etc..
假设我有这段代码(不要介意 SecondsToMinutes 和 MinutesToHours 是彼此的副本)
inline float SecondsToMinutes(float seconds)
{
return seconds / 60.0;
}
inline float MinutesToHours(float minutes)
{
return minutes / 60.0;
}
inline float HoursToDays(float minutes)
{
return minutes / 24.0;
}
inline float SeconndsToHours(float seconds)
{
return MinutesToHours(SecondsToMinutes(seconds));
}
inline float MinutesToDays(float minutes)
{
return HoursToDays(MinutesToHours(minutes));
}
inline float SeconndsDays(float seconds)
{
return MinutesToDays(SecondsToMinutes(seconds));
}
这是内联的有效用法吗?是否有意义?这是好习惯吗?毕竟,如果我没记错的话,inline是指函数调用被函数体代替了,所以
return MinutesToDays(SecondsToMinutes(seconds))
应该等同于
return seconds / 60.0 / 60.0 / 24.0
对吗?
还是只使用宏更好?
#define EXCHANGE_SEC_MIN(x) (x / 60.0)
#define EXCHANGE_MIN_H(x) (x / 60.0)
#define EXCHANGE_H_D(x) (x / 24.0)
#define EXCHANGE_SEC_H(x) (EXCHANGE_MIN_H(EXCHANGE_SEC_MIN(x)))
#define EXCHANGE_MIN_D(x) (EXCHANGE_H_D(EXCHANGE_MIN_H(x)))
#define EXCHANGE_SEC_D(x) (EXCHANGE_MIN_D(EXCHANGE_SEC_MIN(x)))
哪种做法更好?或者两者都不是?我希望其他人对此有美分。
Is this valid usage of inline? Does it make sense? Is this good practice? After all, if I recall correctly, inline means that function calls are replaced by function bodies, so
当然是。你让它更清晰,这总是好的。
return seconds / 60.0 / 60.0 / 24.0
是的,结果就是这样。或者应该。 inline
只是一个提示,编译可能会做出其他决定。但是对于这样一个liner,编译器会内联它。
宏?为什么?函数都能搞定,为什么要用宏?
inline
是不是意思是函数调用被函数体代替了。至少这并不意味着在过去的 fifteen-something 年里:优化器现在已经远远超出了接受开发人员的命令,并且无论您是否指定 inline
.
inline
其实就是"this function may be defined multiple times, and the linker should sort it out and keep at most a single definition at the end. I'm responsible for ensuring that all definitions are identical".
如果你真的,真的想要自己强制执行内联(函数的 body 在调用者内部的实际插入),你将不得不使用 compiler-specific 扩展名,例如 __attribute__((always_inline))
.
当函数在 header 中定义时,您通常需要 inline
,因为这个 header 最终将包含在多个翻译单元中,因此定义将被重复。因此,假设您的代码在 header 中,这是 inline
.
假设您的内联函数的定义在使用时对编译器可见(例如,它们位于每个编译单元需要的 #include
d 头文件中),那么您的用法是有效。
然而,inline
只是对编译器的提示。标准允许编译器忽略该提示,而不是内联该函数。编译器不内联函数的标准高度依赖于编译器。
宏是另一种选择,但还有其他问题....包括它们不尊重程序范围,并且很容易编写宏 - 有意或无意 - 导致与人们预期不同的行为.所以内联函数通常被认为是更可取的。
Is this valid usage of inline? Does it make sense?
嗯,是的,但不是。
此时它不会造成任何伤害,但也不会像您认为的那样。
在 excellent post about inline
deft_code 中正确地说:
It is said that
inline
hints to the compiler that you think the function should be inlined. That may have been true in 1998, but a decade later the compiler needs no such hints. Not to mention humans are usually wrong when it comes to optimizing code, so most compilers flat out ignore the 'hint'.
因此,如果您这样做不会伤害任何人,但您的编译器听取您的提示的机会几乎为 0。如果它认为适合内联代码,它会自己这样做。
inline
现在主要用于链接器,因为它允许在多个编译单元中进行多个定义。
如果你想确保你的代码尽可能快并且你可以访问 C++11,你应该使用 constexpr
:
constexpr float SecondsToMinutes(float seconds)
{
return seconds / 60.0;
}
//etc..