5

Why can't i

if (IUnknownPtr p = anotherComPtr) {} //error C2275: 'IUnknownPtr' : illegal use of this type as an expression

while i can

if (int* a = anotherPointer) {}

IUnknownPtr is defined throught _COM_SMARTPTR_TYPEDEF(IUnknown, __uuidof(IUnknown)) (like any others smart pointers i use)

How can i create a com smartptr within if statement and verify is it valid or not? Thank you.

I use VS 2008

p.s. This is not about is it good way of coding or not, it's about error C2275.

8
  • This is weird. Which version of Visual C++ do you use? Commented Feb 14, 2012 at 10:55
  • Why are you not creating the pointer outside the if? And check for validity in the if? Or is the obvious in COM context? (No experience) Commented Feb 14, 2012 at 11:50
  • Tried on VS2k8 and VS2k10 - works just fine. Commented Feb 14, 2012 at 11:54
  • @PoweRoy: i think my way if more ellegant - to automatically release a pointer after if statement end. I wouldn't like to wrap such construction into additional {}. Commented Feb 14, 2012 at 12:05
  • @sharptooth: i know it must works fine, but doesn't. This is why i've posted the question. Anyway, thank you for checking. Commented Feb 14, 2012 at 12:07

3 Answers 3

1

I can't reproduce your compiler error in vs2008 in the small program below. There is likely to be something different in your include files, preprocessor definitions or compiler options which is giving you different behaviour.

Can you declare a simple variable of type IUnknownPtr outside an if statement?

Can you create a new project using the code below without the error?

Does either of the following compile OK?

if (NULL == (IUnknownPtr ptr = someOtherPtr)) {
}

IUnknownPtr foo;
bool b(foo);

The error suggests the compiler can see a definition of IUnknownPtr but can't interpret the result of the assignment of an IUnknownPtr as a bool.

operator = should return IUnknownPtr& (the object that's been assigned to). _com_ptr_t defines operator bool(). Are your _COM_SMARTPTR_TYPEDEF generating references to _com_ptr_t or some other type? You can easily find out by temporarily dumping out the preprocessor output (properties/C++/preprocessor/preprocess to a file)

#include <comdef.h>

int main(int argc, char* argv[])
{
    IUnknownPtr foo;
    IUnknown* foo2 = NULL;

    if (IUnknownPtr foo3 = foo) {
        // do something
    }

    if (IUnknownPtr foo4 = foo2) {
        // do something
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I vaugely remember something about MSDEV not following the C++ specs when it came to declaring a variable inside an if statement (or it might have been a for loop?).

It should have been destructed at the end of the brackets but it was not.

Possibly this is a throwback to that - have you tried combing the MSDN archives? It seems more like a quirk than intentional behaviour, unless (as another poster commented) you have misunderstood what is happening.

Can you post the actual code, complete with surrounding function?

1 Comment

You are correct, but it was the for statement, and it's been fixed at some point (it's definitely fixed in VS2010). The 'if' should work.
0

Since this compiles for me just fine on VS 2010, I'm going to assume maybe you didn't #include <comdef.h> and/or #include <comdefsp.h>.

Comments

Your Answer

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.