operator bool and warning C4213

M

mikron30

In the following code I am getting the following warning
warning C4213: nonstandard extension used : cast on l-value

class X
{
public:
operator bool() {};
};

class Y
{
public:
bool b;
};

void main()
{
Y y;
X x;
y.b = x; // WARNING here
}

I can work around the problem by casting
y.b = (bool)x; // No Warning

If I declare local variable the warning disapear
bool b = x; // NO WARNING !

I am working with Visual Studio 6 SP 6.

What is going on here?

Thanks,
Miki Zilbershtein.
 
C

Christian Meier

In the following code I am getting the following warning
warning C4213: nonstandard extension used : cast on l-value

class X
{
public:
operator bool() {};
};

class Y
{
public:
bool b;
};

void main()

int main()
{
Y y;
X x;
y.b = x; // WARNING here
}

I can work around the problem by casting
y.b = (bool)x; // No Warning

If I declare local variable the warning disapear
bool b = x; // NO WARNING !

I am working with Visual Studio 6 SP 6.

What is going on here?

Thanks,
Miki Zilbershtein.

I do not get a warning using g++ 3.3.
What happens if you return a value in operator bool()?

operator bool() {return true; }


Greetings
Chris
 
J

John Carson

In the following code I am getting the following warning
warning C4213: nonstandard extension used : cast on l-value

class X
{
public:
operator bool() {};
};

class Y
{
public:
bool b;
};

void main()
{
Y y;
X x;
y.b = x; // WARNING here
}

I can work around the problem by casting
y.b = (bool)x; // No Warning

If I declare local variable the warning disapear
bool b = x; // NO WARNING !

I am working with Visual Studio 6 SP 6.

What is going on here?

Thanks,
Miki Zilbershtein.

VC++ 7.1 won't even compile your code. operator bool() is supposed to return
true or false. It makes no sense at the moment.
 
I

Ian

In the following code I am getting the following warning
warning C4213: nonstandard extension used : cast on l-value

class X
{
public:
operator bool() {};

Any C++ compiler that swallows this must be broken, you have to return
something!
};

class Y
{
public:
bool b;
};

void main()

Any C++ compiler that swallows this must be broken, main has to have a
return type of int.

Ian
 
P

Pete C

Ian said:
Any C++ compiler that swallows this must be broken, you have to return
something!

No, this is undefined behaviour but is not a compilation error. From
6.6.3.2: "flowing off the end of a function is equivalent to a return
with no value; this results in undefined behaviour in a value-returning
function".

For example, g++ will give a warning if you specify -Wall, but will
still compile the code. I've been caught out myself out a few times by
that, but it's technically correct.
 
M

mikron30

Thanks for your answers, byt the return value is not the problem.

I changed the source to

class X
{
public:
operator bool() {return true;}; // Changed !!!
};

class Y
{
public:
bool b;
};

void main()
{
Y y;
X x;
y.b = x;
}

But the warning stays the same.
This code is just a simple example to show my problem.
 
J

John Carson

Thanks for your answers, byt the return value is not the problem.

I changed the source to

class X
{
public:
operator bool() {return true;}; // Changed !!!
};

class Y
{
public:
bool b;
};

void main()
{
Y y;
X x;
y.b = x;
}

But the warning stays the same.
This code is just a simple example to show my problem.

Then it is a compiler problem. There is nothing wrong with the code (except
for the void return for main() ). VC++ 7.1 compiles it without a problem as
does VC++ 8.0 (in beta) and Comeau.
 
M

mikron30

Thanks.

I was suspecting it is a compiler problem.

I will #pragma disable this warning.
 
I

Ian

Pete said:
No, this is undefined behaviour but is not a compilation error. From
6.6.3.2: "flowing off the end of a function is equivalent to a return
with no value; this results in undefined behaviour in a value-returning
function".
Then I'd suggest the standard is broken in this area.
For example, g++ will give a warning if you specify -Wall, but will
still compile the code. I've been caught out myself out a few times by
that, but it's technically correct.
It should warn is all cases?

Sun CC treats this as an error.

Ian
 
P

Pete C

Ian said:
Then I'd suggest the standard is broken in this area.

I agree, but I'm no expert. I'd be interested if anyone here could give
a justification for this behaviour. Maybe a C compatibility quirk... or
perhaps it was considered too burdensome to require compilers to detect
whether control could possibly reach the end of a function.
It should warn is all cases?

Again I agree, a warning would always be nice.
Sun CC treats this as an error.

Personally, I believe that compliant code should always compile under a
compiler's default settings. With warnings if need be. Sun (and
Microsoft too) are forsaking standards in favour of making life
safer/easier. People will always disagree over where to draw that line.
 

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

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top