3

I was trying to create a small define to work around this bug in QITABENT and I noticed peculiar behavior of the #pragma warning (disable: ...) statement.

In the following code the define QITABENT generates warning C4838

static const QITAB qit[] = 
{
 QITABENT(MediaPlayerCallback, IMFPMediaPlayerCallback)
};

I can easily suppress this warning, this works:

#pragma warning( push )
#pragma warning( disable: 4838 )
 static const QITAB qit[] = 
 {
 QITABENT(MediaPlayerCallback, IMFPMediaPlayerCallback)
 //{ 0 },
 };
 return QISearch(this, qit, riid, ppv);
#pragma warning( pop )

Now I wanted to make a define QITABENTEX which automatically suppresses the warning generated by QITABENT. But it seems impossible because when I write the following code the warning C4838 is not suppressed.

 static const QITAB qit[] = 
 {
#pragma warning( push )
#pragma warning( disable: 4838 )
 QITABENT(MediaPlayerCallback, IMFPMediaPlayerCallback)
#pragma warning( pop )
 //{ 0 },
 };

How does the compiler interpret this code in such a way that the warning is not suppressed?

This probably has to with when the definition for QITABENT is fully resolved.

(Note that I'm not really interested in making the above code work, I'm just really curious how its interpreted by the compiler)

Addendum:

Regarding the close vote and clarification: I got into a discussion with someone giving a shotgun/link-only answer, presumably only reading the question halfway through (since the question explained how to use #pragma in a macro which is not what I'm asking) now that answer got (self) deleted and I got a close vote for being unclear. So let me reiterate my intentions with this question:

  • This question is not about finding a define to suppress this warning
  • This question is not about how to suppress a warning in VC++ in general

  • This question is about trying to understand what happens with the three lines of suppression code in the last code sample. Why do they not have effect in that exact position (in an array initialization) but do have effect outside the array initialization? This probably boils down to answering how and when the pragma statements and macross are resolved.

asked Jan 11, 2016 at 13:47
8
  • Please post the entire C4838 warning message generated by this piece of code. Commented Jan 11, 2016 at 14:20
  • @VioletGiraffe the warning is irrelevant (and doesn't change when I move the pragma statements) the interesting bit is why the pragma is ignored at that particular spot :). Commented Jan 11, 2016 at 14:50
  • are you sure the warning (C4838 is a narrowing conversion warning) is not in the return QISearch(...) line (in your first suppression example), instead of in the actual struct definition? Commented Jan 11, 2016 at 15:25
  • Note that standard C has _Pragma but Microsoft C has __pragma instead (per __pragma keyword). However it is spelled, it appears that the pragma operator (as opposed to preprocessor directive, #pragma) does not help. Commented Jan 11, 2016 at 15:52
  • It is up to the compiler to interpret pragmata; it does it however it likes. If it doesn't do it the way you'd like, you're almost certainly stuck with it. You can try reporting the issue to Microsoft (the compiler vendor), but the chances of change as a result are slim to negligible, I fear. It may be that the suppression can only be turned on before the braces of the initializer, or only before the = of the initializer, or it may need to be before the start of the declaration. There could be reasons for any or all of those choices (which you might not always agree with). Commented Jan 11, 2016 at 15:56

1 Answer 1

6

The initializer list terminates with the closing curly bracket }, and that is where warning is generated.

Try this:

static const QITAB qit[] =
{
 QITABENT(Derived, Base)
#pragma warning( push )
#pragma warning( disable: 4365 )
}
#pragma warning( pop )
;

[edited] Per Mr.C64's comment below, corrected the order of parameters in QITABENT(Derived, Base).

answered Jan 11, 2016 at 22:59
Sign up to request clarification or add additional context in comments.

4 Comments

Ah that's an interesting way to place the suppression code. Surprised that works! Gives me some insight in how everything is resolved.
I think you have a typo in the code in the answer, and that should be QITABENT(Derived, Base) (or QITABENT(ClassName, InterfaceName)).
I didn't want to edit your answer out of respect; I just wanted you to fix the typo. I found your answer useful and upvoted it.
I appreciate that; corrected the code in my answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.