CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,784

    inline code - what are MSVC's "rules"

    We all know that Visual Studio is trying to support non-Microsoft compilers. I don't know how many are supported but Clang is one of them. Consider this code:-

    Code:
    #if defined (BUILDING_DLL)
      #define DLL_API __declspec(dllexport)
    #else
      #define DLL_API __declspec(dllimport)
    #endif
    
    namespace Gtkmm2ext {
    
      class DLL_API Keyboard
      {
        public:
          Keyboard ();
          ~Keyboard ();
    
          static Keyboard& get_keyboard() { return *_the_keyboard; }
    
        protected:
          static Keyboard* _the_keyboard;
      };
    
    } /* namespace */
    If the above code appears in a header file, MSVC builds it just fine. But Clang has a habit of inling anything that gets defined in a header file. And of course for anything declared as __declspec(dllimport) that causes huge problems (e.g. functions like get_keyboard() which compile but won't then link...) The Clang devs are willing to fix this but they wanted to know how MSVC handles it (i.e. what the "rules" are). So I asked a question on Microsoft's Developer Community forum. But because Clang isn't a MS compiler, Microsoft just closed the thread!!

    So does anyone know how MSVC inlines code? Back in the old days I remember that code would never be inlined if it wasn't declared using inline - and even if it was, inline was only considered as a hint to the compiler. The compiler was free to ignore it. Is that still the case for MSVC or are the rules more complicated these days?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,566

    Re: inline code - what are MSVC's "rules"

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.2)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,784

    Re: inline code - what are MSVC's "rules"

    Thanks 2kaud - the 3rd link leads to a page with 2 x examples. And the 2nd example states that any function that's defined within a header file is now regarded as being implicitly inlined. But what I can't find out is whether that also apples to DLL functions. My experience suggests that it doesn't - but I just can't find it in writing anywhere...
    "A problem well stated is a problem half solved. - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured