What does extern inline do?

后端 未结 6 2157
闹比i
闹比i 2020-11-22 12:07

I understand that inline by itself is a suggestion to the compiler, and at its discretion it may or may not inline the function, and it will also produce linkab

6条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 12:37

    It sounds like you're trying to write something like this:

    inline void printLocation()
    {
      cout <<"You're at " __FILE__ ", line number" __LINE__;
    }
    
    {
    ...
      printLocation();
    ...
      printLocation();
    ...
      printLocation();
    

    and hoping that you'll get different values printed each time. As Don says, you won't, because __FILE__ and __LINE__ are implemented by the preprocessor, but inline is implemented by the compiler. So wherever you call printLocation from, you'll get the same result.

    The only way you can get this to work is to make printLocation a macro. (Yes, I know...)

    #define PRINT_LOCATION  {cout <<"You're at " __FILE__ ", line number" __LINE__}
    
    ...
      PRINT_LOCATION;
    ...
      PRINT_LOCATION;
    ...
    

提交回复
热议问题