简体   繁体   中英

Does extern matter for function declaration with inline?

As specified in the Standard

If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern .

But function specifier part gives inline function semantic as follows:

Any function with internal linkage can be an inline function. For a function with external linkage, the following restrictions apply: If a function is declared with an inline function specifier, then it shall also be defined in the same translation unit. If all of the file scope declarations for a function in a translation unit include the inline function specifier without extern , then the definition in that translation unit is an inline definition.

Case 1.

static inline void test(void){ //internal linkage, can be an inline function
    printf("Test\n");
}

inline void test(void); //does it provide an external definition?

Case 2.

static inline void test(void){ //internal linkage, can be an inline function
    printf("Test\n");
}

extern inline void test(void); //does it provide an external definition?

Case 3.

static inline void test(void){ //internal linkage, can be an inline function
    printf("Test\n");
}

void test(void); //does it provide an external definition?

I have a confusion regarding the three cases. Are there differences between them? I currently think about them as

Case 1 -- does not provide an external definition ( inline without extern )

Case 2 -- provides external definition ( inline with extern )

Case 3 -- provides external definition (same as with extern )

static and extern cannot go together.

static inline void test(void){ //internal linkage, can be an inline function
    printf("Test\n");
}

inline void test(void); //does it provide an external definition?

should actually be

static inline void test(void){ //internal linkage, can be an inline function
    printf("Test\n");
}

static inline void test(void); //does it provide an external definition?

because the definition and the declaration should match. I am not sure though that static actually needs to be used when also using inline .


Case 2 -- provides external definition (inline with extern)

Case 3 -- provides external definition (same as with extern)

these actually conflict (if I understand correctly) with:

Any function with internal linkage can be an inline function.

extern is exactly about external linkage, as opposed to internal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM