정적 인라인 함수를 사용해서는 안됩니까?
inline
키워드 (§ 7.1.3 / 4) 사용에는 두 가지 의미가 있습니다 .
- 이 힌트 호출의 시점에서 기능 체의 교체를 일반적인 함수 호출기구 위에 바람직하다 컴파일러.
- 인라인 대체가 생략 되더라도 inline에 대한 다른 규칙 (특히 wrt One Definition Rule )을 따릅니다.
일반적으로 모든 주류 컴파일러는 필요한 경우 호출 지점에서 함수 본문을 대체하므로 inline
단순히 for 함수를 표시하는 #1
것은 실제로 필요하지 않습니다.
추가 wrt #2
, 함수를 static inline
함수 로 선언 할 때 이해했듯이 ,
static
함수 의 키워드는 함수 inline
가 내부 연결을 갖도록 강제합니다 ( 인라인 함수에는 외부 연결이 있음 ) 이러한 함수의 각 인스턴스는 별도의 함수로 처리되며 ( 각 함수의 주소는 다릅니다 ) 이러한 함수의 각 인스턴스에는 자체 복사본이 있습니다. 정적 지역 변수 및 문자열 리터럴 ( 인라인 함수에는 이러한 복사본이 하나만 있음 )
따라서 이러한 기능은 다른 static
기능 처럼 작동하며 키워드 inline
는 더 이상 중요하지 않으며 중복됩니다.
따라서 실제로 기능을 표시 static
하고 inline
둘 다 전혀 사용하지 않습니다. 그것은 static
( 가장 선호하지 않음 ) 또는 inline
( 가장 선호 됨 ) 이어야합니다 .
그래서 함수에 static
and inline
함께 사용하는 것은 실질적으로 쓸모가 없습니까?
귀하의 분석은 정확하지만 반드시 쓸모가 없음을 의미하지는 않습니다. 대부분의 컴파일러가 자동으로 인라인 함수를 수행하더라도 (이유 # 1) inline
의도를 설명 하기 위해 선언하는 것이 가장 좋습니다 .
와의 상호 작용을 무시 inline
하고 static
함수를 드물게 사용해야합니다. static
네임 스페이스 범위 의 한정자는 이전에 이름이 지정되지 않은 네임 스페이스를 위해 사용되지 않습니다 (C ++ 03 §D.2). 기억할 수없는 모호한 이유로 C ++ 11의 지원 중단에서 제거되었지만 거의 필요하지 않습니다.
따라서 실제로 함수를 정적 및 인라인으로 표시하는 것은 전혀 사용되지 않습니다. 정적 (가장 선호되지 않음) 또는 인라인 (가장 선호 됨)이어야합니다.
선호에 대한 개념이 없습니다. static
동일한 서명을 가진 다른 기능이 다른 .cpp
파일 (번역 단위)에 존재할 수 있음을 의미합니다 . inline
없이는 static
다른 번역 단위가 동일한 정의로 동일한 기능을 정의하는 것이 좋습니다.
다음 대신 이름이 지정되지 않은 네임 스페이스를 사용하는 것이 좋습니다 static
.
namespace {
inline void better(); // give the function a unique name
}
static inline void worse(); // kludge the linker to allowing duplicates
정적 및 인라인은 직교 (독립)입니다. 정적은 함수가 번역 단위 외부에서 보이지 않아야 함을 의미하고, 인라인은 프로그래머가이 함수를 인라인하기를 원하는 컴파일러에 대한 힌트입니다. 이 두 가지는 관련이 없습니다.
static inline
인라인 함수가 번역 단위 외부에서 사용되지 않을 때 사용하는 것이 좋습니다. 이를 사용하면 동일한 이름의 다른 변환 단위에있는 다른 인라인 함수의 이름을 지정하여 실수로 ODR 규칙을 위반하는 상황을 방지 할 수 있습니다.
예:
source1.cpp :
inline int Foo()
{
return 1;
}
int Bar1()
{
return Foo();
}
source2.cpp :
inline int Foo()
{
return 2;
}
int Bar2()
{
return Foo();
}
Without using static on Foo (or without using an anonymous namespace, which is preferred way by most C++ programmers), this example violates ODR and the results are undefined. You can test with Visual Studio the result of Bar1/Bar2 will depend on compiler settings - in Debug configuration both Bar1 and Bar2 will return the same value (inlining not used, one implementation selected randomly by the linker), in Release configuration each of them will return the intended value.
I may not be completely right about this, but as far as I know declaring a function static inline
is the only way to make (or allow) the compiler to generate a machine code where the function really is not defined in the compiled code at all, and all you have is a direct substitution of the declared function into a sequence of instructions, like it were just a regular procedure body, with no trace in the machine code of a procedure call relative to that function definition from the source code.
That is, only with static inline
you can really substitute the use of a macro, inline
by itself is not enough.
A simple Google search for "static inline" will show you compiler documentation pages that talk about it. I guess this should be enough to answer your question, and say, "no, it is not practically useless". Here is one example of a site discussing the use of inline
, and specifically of static inline
http://www.greenend.org.uk/rjk/tech/inline.html
If you talk about free functions (namespace
scope), then your assumption is correct. static inline
functions indeed don't have much value. So static inline
is simply a static
function, which automatically satisfies ODR and inline
is redundant for ODR purpose.
However when we talk about member methods (class
scope), the static inline
function does have the value.
Once you declare a class
method as inline
, it's full body has to be visible to all translation units which includes that class
.
Remember that static
keyword has a different meaning when it comes for a class
.
Edit: As you may know that static
function inside a class
doesn't have internal linkage, in other words a class cannot have different copies of its static
method depending on the translation (.cpp) units.
But a free static
function at namespace
/global scope does have different copies per every translation unit.
e.g.
// file.h
static void foo () {}
struct A {
static void foo () {}
};
// file1.cpp
#include"file.h"
void x1 ()
{
foo(); // different function exclusive to file1.cpp
A::foo(); // same function
}
// file2.cpp
#include"file.h"
void x2 ()
{
foo(); // different function exclusive to file2.cpp
A::foo(); // same function
}
I just read a man page for gcc and it specifically states the use of static inline with a compiler flag. In the case of the flag, it inlines the function and if it is also static and is inlined in every instance that it is called, then it gets rid of the function definition which will never be used in the created object file, thereby reducing the size of the generated code by that little bit.
참고URL : https://stackoverflow.com/questions/10876930/should-one-never-use-static-inline-function
'Nice programing' 카테고리의 다른 글
USB 디버깅 중에 화면을 잠금 해제 상태로 유지하려면 어떻게합니까? (0) | 2020.12.08 |
---|---|
Clang에서 C ++ 11 기능을 어떻게 사용할 수 있습니까? (0) | 2020.12.08 |
project.xcworkspace 파일이 중요합니까? (0) | 2020.12.08 |
여러 줄에 사전 pprint (0) | 2020.12.07 |
package-info.java가 유용한 이유는 무엇입니까? (0) | 2020.12.07 |