473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

operator bool and warning C4213

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.

Sep 6 '05 #1
9 2916
<mi******@gmail.com> schrieb im Newsbeitrag
news:11**********************@o13g2000cwo.googlegr oups.com...
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
Sep 6 '05 #2
<mi******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com
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.

--
John Carson

Sep 6 '05 #3
Ian
mi******@gmail.com wrote:
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
Sep 6 '05 #4
Ian wrote:
operator bool() {};


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.

Sep 6 '05 #5
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.

Sep 6 '05 #6
<mi******@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com
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.
--
John Carson

Sep 6 '05 #7
Thanks.

I was suspecting it is a compiler problem.

I will #pragma disable this warning.

Sep 6 '05 #8
Ian
Pete C wrote:
Ian wrote:
operator bool() {};
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".

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
Sep 7 '05 #9
Ian wrote:
Pete C wrote:
No, this is undefined behaviour but is not a compilation error.
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.

Sep 7 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Xavier Decoret | last post by:
The following code does not compoile with gcc-3.2.3 namespace dummy { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Interface of Foo...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
2
by: Alex Vinokur | last post by:
Hi, GNU g++ 3.4 detects an error in code below. What is wrong here? --------- foo.cpp : BEGIN --------- template <typename T> struct Foo {
8
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
10
by: olson_ord | last post by:
Hi, I am not exactly new to C++, but I have never done operator overloading before. I have some old code that tries to implement a Shift Register - but I cannot seem to get it to work. Here's a...
12
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
5
by: David | last post by:
Hi all, I am trying to overload the < operator, but get warning class Windowinfo { protected: HWND wndhandle; //the window handle int wndId; //the window Id
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.