开发者

Does a function local static variable prevent function inlining?

I'm writing an attribute shim to get the raw c-string from a library's string implementation. This specific string class, say string_t has members length() and data(). When length() == 0 data() == nullptr.

Now I am using an a开发者_如何学JAVApi that doesn't like null strings so my shim returns the address of an empty string.

inline char const* get_safe_c_str( string_t const& str ){
    static char const empty[] = "";
    return str.length() > 0 ? str.data() : ∅
}

Does my static variable prevent the compiler from inlining this function?


No, it does not prevent inlining. There will still be only one instance of the function-local static variable, and everywhere the function is expanded inline, that instance will be used.

Whether a particular compiler with particular options actually inlines such a function is another matter, and you'd have to compile your program to see what your compiler actually does, but there is no technical reason the function can't be inlined.

Note, however, that in your program, return str.length() > 0 ? str.data() : ""; would also work fine; a string literal has static storage duration and exists until the program terminates.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜