bool native->managed conversion

T

ttomic

Consider this CLI example from VS2005 SP1:

// ************************************

using namespace System;

#pragma unmanaged

struct S {
int x;
S() : x(0) {}
typedef void(S::*unspecified_bool_type)();
operator unspecified_bool_type() const { return x != 0 ?
&S::Dummy : 0; }
void Dummy() {}
};

inline int ret_bool(const S& s) {
return bool(s);
}

#pragma managed

int main () {
S s;
// if(ret_bool(s))
if(s)
Console::WriteLine("true");
else
Console::WriteLine("false");
return 0;
}

// ********************************************************

Program will output "true".
If we replace "if(s)" with "if(ret_bool(s))" program will output
correct string: "false".

It look's like
http://support.microsoft.com/default.aspx?kbid=823071
but I thought that was fixed long time ago.

Should I pollute my code with "__asm mov eax, 0;" again ?
Can someone help me?
 
B

Ben Voigt [C++ MVP]

Consider this CLI example from VS2005 SP1:

// ************************************

using namespace System;

#pragma unmanaged

struct S {
int x;
S() : x(0) {}
typedef void(S::*unspecified_bool_type)();
operator unspecified_bool_type() const { return x != 0 ?
&S::Dummy : 0; }
void Dummy() {}
};

inline int ret_bool(const S& s) {
return bool(s);
}

#pragma managed

int main () {
S s;
// if(ret_bool(s))
if(s)
Console::WriteLine("true");
else
Console::WriteLine("false");
return 0;
}

// ********************************************************

Program will output "true".
If we replace "if(s)" with "if(ret_bool(s))" program will output
correct string: "false".

Sections 4.11P1, 4.12P1, and 6.4P5 indicate to me that your code is correct.
However since pointer-to-member-functions often use a _representation_ of
zero for a real member, and another representation for the null member
pointer, I would try using a plain function pointer instead of a pointer to
member.

For example:

struct S {
int x;
S() : x(0) {}
typedef void (*unspecified_bool_type)(S*);
operator unspecified_bool_type() const { return (x != 0) ? &S::Dummy :
0; }
static void Dummy(S*) {}
};
 
T

Tonci Tomic

Sections 4.11P1, 4.12P1, and 6.4P5 indicate to me that your code is correct.
However since pointer-to-member-functions often use a _representation_ of
zero for a real member, and another representation for the null member
pointer, I would try using a plain function pointer instead of a pointer to
member.

For example:

struct S {
    int x;
    S() : x(0) {}
    typedef void (*unspecified_bool_type)(S*);
    operator unspecified_bool_type() const { return (x != 0) ? &S::Dummy :
0; }
    static void Dummy(S*) {}
 };

You are right. Thx.
 
B

Ben Voigt [C++ MVP]

Tonci Tomic said:
You are right. Thx.

I'm glad we found a workaround for you. Please do go ahead and file a bug
on Connect though so that Microsoft can fix this in a future compiler
version.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top