bool native->managed conversion

  • Thread starter Thread starter ttomic
  • Start date Start date
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?
 
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*) {}
};
 
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.
 
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.
 
Back
Top