Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7673763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:35:14+00:00 2026-05-31T16:35:14+00:00

When compiling the code below with cl /c /clr /W4 the compiler says warning

  • 0

When compiling the code below with cl /c /clr /W4 the compiler says

warning C4793: 'Interface::'vcall'{0}'' : function compiled as native

So the pragma does not seem to have any effect.. Is there a way to fix this? Or is this a bug (the pragma does work with a non-template class)? Can this warning safely be disabled?

#pragma unmanaged

struct Interface
{
  virtual void Foo() = 0;
};

template< class T >
struct UsesFunPtr
{
  UsesFunPtr()
  {
    &T::Foo;
  }
};

void DoIt()
{
  UsesFunPtr< Interface > a;
}

#pragma managed

Update: if I remove the last line the warning goes away – so following ComicSansMs’ answer: when exactly is at the time of definition for the template? Can anyone explain why the last line, after which no code follows, still affects the code before it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:35:15+00:00Added an answer on May 31, 2026 at 4:35 pm

    Big revision, since (in the comments) the poster explains why &T:Foo; was not an issue.

    The origins of this warning are somewhat stupidly complex.

    After investigation we find the following from Microsoft, which ComicSansMS also posted:

    When a template function is instantiated, the pragma state at the time of definition for the template determines if it is managed or unmanaged.

    This is for a function template, not for a class template as you are using.

    In fact, this function template instantion is relevant in a circumspect way. But the peculiar way this warning behaves has to do with the compilation process.

    The compiler is actually generating managed code for calling and using the UsesFunPtr ctor when you end the file it with #pragma managed. It gives a warning that there’s some unmanaged code in it. Here’s an in-depth analysis of why.

    Thunks are basically wrapper functions around certain vtable calls; Wikipedia has a decent article on the subject. The reason you are generating a thunk is because you are taking the address of a function (&T::Foo).

    If the last line of your file is this:

    #pragma unmanaged 
    

    it will stop complaining, even if you have mixed managed and unmanaged code in that object. It is because of a disconnect between the front-end and back-end that “confuses” things: it will compile that code I spoke of above with the last #pragma command.

    If you’ll notice, this warning is not a compile-time warning. It comes after the compile, when it says “Generating code…” If you revise your DoIt() function like so:

    void DoIt()
    {
      long a;
      long long b;
      b = 4;
      a = b;
      UsesFunPtr< Interface > d;
    }
    

    You will get a warning about truncation during “Compiling x.cpp”, then it will move to the code generation phase (Generating code…), where it will give the warning this question is about.

    The compiler is the front-end which parses, etc. and creates a sort-of intermediate binary format, kind of like Java bytecode. The code generator is the back-end which creates the actual output on the target platform from this bytecode (Windows x86 in this case).

    Optimizations aren’t made until the back-end gets hold of the code. A thunk is a form of optimization, thus, it is the code generator (that takes semi-compiled IL code) that makes the warning, and not the compiler. It is not an optimization that can be turned off in any way I know of, because it is sort of a standard practice.

    It is the compiler, however, that instantiates that template; the back-end just sees a complete class and is told to make sense of it.

    There is a difference when compiling managed C++/CLI. Sometimes the compiler is able to use what’s known as link-time code generation. When this happens, the linker is the one to call the back-end; when it isn’t possible (for various reasons), it goes through the generic process of compiling (front end->back end->linker).

    A #pragma is passed into the IL in the order it is received. This seems to indicate that the function support code for UsesFunPtr is actually “appended to the end,” after the #pragma managed has happened. So even though your code is in an unmanaged space, the code generator sees:

    1. Unmanaged
    2. Object definition, use
    3. Managed
    4. Additional support code for the classes that you didn’t write and can’t see that has to do with interfacing with other object files: creating and copying vtables, etc. etc. etc.

    The generator has no way to differentiate if you meant that UsesFunPtr ctor, or even class, to be completely managed or unmanaged code, because the IL it gets doesn’t really connect the two. It sees a function that creates it that is unmanaged (by pragma), and support functions that work on it and produce thunks in managed space (by pragma). It can’t tell the connection. Since you put that #pragma there, it is just continuing with the last #pragma it saw. The generated support code is managed.

    You’ll notice that if you put #pragma unmanaged at the very end, even if there’s managed code that mixes the templates, you won’t get that warning. This is because it is compiling that code as unmanaged and thus thunks aren’t an issue.

    How to sum this all up? #pragma managed at the end causes a miscommunication (or would it be a wrong assumption?) between the front end and the back end, and the back end complains about it.

    Wee, that was a fun one!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When compiling the code below, I get the following error: PersonalInformation is not abstract
I'm having trouble compiling the code below. It may also have logical errors, please
I've discovered (below) that I need to use -fblocks when compiling code which uses
Commented code below are the reasons I think the code is not compiling, is
My code is the below, it's working correctly but, but after compiling program i
I am compiling the code below when the following erro comes up. I am
I am dynamically compiling code in my client application. When I start the application
Take this non-compiling code for instance: public string GetPath(string basefolder, string[] extraFolders) { string
I'm getting a lot of errors compiling code using the boost libraries, mainly when
I'm compiling some code which uses libcurl on a Debian Linux system. My dev

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.