CG_INLINE做什么? [英] What does CG_INLINE do?

查看:266
本文介绍了CG_INLINE做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览 CGPoint 的定义,以了解如何创建自己的函数,但我不知道 CG_INLINE

I was poking around the definitions for things like CGPoint for hints on how to create my own functions but I don't know the purpose of CG_INLINE. What is happening behind the scenes here?

CG_INLINE CGPoint
CGPointMake(CGFloat x, CGFloat y)
{
  CGPoint p; p.x = x; p.y = y; return p;
}

CG_INLINE CGSize
CGSizeMake(CGFloat width, CGFloat height)
{
  CGSize size; size.width = width; size.height = height; return size;
}


推荐答案

调用位置,而不是被编译为功能代码的单个块和当使用该功能时发出的调用指令。小心,这提供了更多的速度和更大量的缓存命中。然而,C和C ++中 inline 的历史是摇滚的,所以这个宏有效地提供了一个独立于编译器的 static inline 行为。看定义:

Inline functions are compiled into the call site, rather than being compiled as a single block of function code and call instructions issued when the function is used. With care, this provides a little more speed and greater numbers of cache hits. However, the history of inline in C and C++ is rocky, so this macro effectively provides a compiler independent static inline behaviour. Looking at the definition:

#if !defined(CG_INLINE)
# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
#  define CG_INLINE static inline
# elif defined(__MWERKS__) || defined(__cplusplus)
#  define CG_INLINE static inline
# elif defined(__GNUC__)
#  define CG_INLINE static __inline__
# else
#  define CG_INLINE static    
# endif
#endif /* !defined(CG_INLINE) */


  1. 对于提供适当版本(在这种情况下> = C99)的 __ STDC_VERSION __ ),这意味着 static inline (因为C99允许本机)

  2. 类似地,对于Metrowerks Codewarrior或C ++编译器, $ c> inline 本身。

  3. 对于不支持C99的GCC,它解析为 static __inline __ __ inline __ 的使用是不支持inline的以前C标准的GCC特定内联说明符: http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Alternate-Keywords.html

  4. 如果所有这些都失败了,它就不会打扰inline - 它只是 static

  1. For compilers providing __STDC_VERSION__ of an appropriate version (in this case >= C99), this means static inline (as C99 allows this natively)
  2. Similarly for Metrowerks Codewarrior or C++ compilers, which support inline natively.
  3. For GCCs not supporting C99, it resolves to static __inline__. The use of __inline__ is the GCC specific inline specifier for previous C standards where inline is unsupported : http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Alternate-Keywords.html.
  4. If all of these fail, it doesn't bother with inline - it's just static.

为什么要麻烦所有这些定义?因为苹果在他们的历史上,已经通过了几个编译器。在yore的日子里,Codewarrior C编译器是用户的首选工具。自从OS X以来,苹果一直使用Objective C和C ++通过(原来修改)GCC。最近,他们正在转向。。这个宏覆盖了所有的情况(以及,考虑到新的Core Graphics是什么,我怀疑是旧的宏的修改版本)。

Why bother with all of these definitions? Because Apple, in their history, have been through a fair few compilers. In the days of yore, the Codewarrior C compiler was the tool of choice for users. Since OS X, Apple have been using Objective C and C++ via an (originally modifier) GCC. Recently, they're transitioning to clang. This macro covers all the cases (and, given how new Core Graphics is, I suspect is a modified version of an older macro).

然而,许多编译器将忽略inline注释,因为他们的优化器比程序员提供的提示更好。在你自己的代码中,不要麻烦它(本机形式,或通过这个宏),除非你确实需要它(并已证明它通过分析有用)。当然,您可能仍然希望 static - 上述建议涵盖 inline 行为。

However, many compilers will ignore inline annotations these days, as their optimisers are better than the hints provided by the programmer. In your own code, don't bother with it (in native form, or via this macro) unless you're really sure you need it (and have proven it useful via profiling). Of course, you may still want static - the above advice covers the inline behaviour.

这篇关于CG_INLINE做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆