473,394 Members | 1,709 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,394 software developers and data experts.

Strange compiler warning

I have the following simple program in Solaris Forte compiler 5.4
producing the warning. Though it produces the warning, it works fine
as expected.
This has been compiling fine without any warnings in the older 5.1
compiler. Since the latest compiler produces a warning, it makes me
suspecious about my own code. I still cannot find any problems with it
though.

It essentially produces a warning whenever a copy constructor of a
class with a pure virtual function invokes the pure virtual function
method on the instance that was passed to it.

Eg : -
class X {
public:
virtual void foo() = 0 ;
X (){}
X( X& x) { x.foo() ; }
}

Is this a compiler bug. Or am I wrong somewhere.
Thanks for the help
--sony

quark:/home/ffdfptz/t>CC -V
CC: Forte Developer 7 C++ 5.4 2002/03/09

quark:/home/ffdfptz/t>CC a.C
"a.C", line 13: Warning: Attempt to call a pure virtual function
XXX::foo() cons
t will always fail.
1 Warning(s) detected.
///////////////////////////////////Program

#include<iostream>
#include<string>

using namespace std ;

class XXX {
int i_ ;
virtual int foo() const = 0 ;
public:
XXX(){}
XXX( const XXX& x ) : i_( x.foo() ) {cout<<"hahha"<<i_<<endl ; }
};

class YY : public XXX {

int foo() const { return 13 ; }
public :
YY(){}
YY( const YY& y ) : XXX( y ) {}

};

int main( int argc, char* argv[] ){

YY y ;
YY x ( y ) ;
return 0 ;
}
Jul 19 '05 #1
10 2398
"Sony Antony" <so********@hotmail.com> wrote...
I have the following simple program in Solaris Forte compiler 5.4
producing the warning. Though it produces the warning, it works fine
as expected.
This has been compiling fine without any warnings in the older 5.1
compiler. Since the latest compiler produces a warning, it makes me
suspecious about my own code. I still cannot find any problems with it
though.

It essentially produces a warning whenever a copy constructor of a
class with a pure virtual function invokes the pure virtual function
method on the instance that was passed to it.

Eg : -
class X {
public:
virtual void foo() = 0 ;
X (){}
X( X& x) { x.foo() ; }
}

Is this a compiler bug. Or am I wrong somewhere.


Virtual functions are resolved statically if called from constructors
or destructors. Calling a pure virtual function from a constructor or
a destructor causes undefined behaviour.

Victor
Jul 19 '05 #2
so********@hotmail.com (Sony Antony) wrote in message news:<3e**************************@posting.google. com>...
I have the following simple program in Solaris Forte compiler 5.4
producing the warning. Though it produces the warning, it works fine
as expected.
This has been compiling fine without any warnings in the older 5.1
compiler. Since the latest compiler produces a warning, it makes me
suspecious about my own code. I still cannot find any problems with it
though.

It essentially produces a warning whenever a copy constructor of a
class with a pure virtual function invokes the pure virtual function
method on the instance that was passed to it.

Eg : -
class X {
public:
virtual void foo() = 0 ;
X (){}
X( X& x) { x.foo() ; }
}

Is this a compiler bug. Or am I wrong somewhere.
Thanks for the help
--sony

quark:/home/ffdfptz/t>CC -V
CC: Forte Developer 7 C++ 5.4 2002/03/09

quark:/home/ffdfptz/t>CC a.C
"a.C", line 13: Warning: Attempt to call a pure virtual function
XXX::foo() cons
t will always fail.
1 Warning(s) detected.
///////////////////////////////////Program

#include<iostream>
#include<string>

using namespace std ;

class XXX {
int i_ ;
virtual int foo() const = 0 ;
public:
XXX(){}
XXX( const XXX& x ) : i_( x.foo() ) {cout<<"hahha"<<i_<<endl ; }
};

class YY : public XXX {

int foo() const { return 13 ; }
public :
YY(){}
YY( const YY& y ) : XXX( y ) {}

};

int main( int argc, char* argv[] ){

YY y ;
YY x ( y ) ;
return 0 ;
}


Comeau online compiles your code fine, with no warnings.
http://www.comeaucomputing.com/tryitout/

That's not an absolut guarantee of correctness, but there isn't much
it gets wrong. Bear in mind that the compiler is allowed to warn about
anything it likes. Perhaps they are tyring too hard to protect you.

GJD
Jul 19 '05 #3
Just guessing since I have never used that compiler.

When this constructor is running child classes have not yet been
constructed, so you should not call your childrens member functions, such
execution is I think undefined behaviour. Perhaps future versions of your
compiler may not allow this to happen. Which foo are you expecting to run
from here?

Tom

"Sony Antony" <so********@hotmail.com> wrote in message
news:3e**************************@posting.google.c om...
I have the following simple program in Solaris Forte compiler 5.4
producing the warning. Though it produces the warning, it works fine
as expected.
This has been compiling fine without any warnings in the older 5.1
compiler. Since the latest compiler produces a warning, it makes me
suspecious about my own code. I still cannot find any problems with it
though.

It essentially produces a warning whenever a copy constructor of a
class with a pure virtual function invokes the pure virtual function
method on the instance that was passed to it.

Eg : -
class X {
public:
virtual void foo() = 0 ;
X (){}
X( X& x) { x.foo() ; }
}

Is this a compiler bug. Or am I wrong somewhere.
Thanks for the help
--sony

quark:/home/ffdfptz/t>CC -V
CC: Forte Developer 7 C++ 5.4 2002/03/09

quark:/home/ffdfptz/t>CC a.C
"a.C", line 13: Warning: Attempt to call a pure virtual function
XXX::foo() cons
t will always fail.
1 Warning(s) detected.
///////////////////////////////////Program

#include<iostream>
#include<string>

using namespace std ;

class XXX {
int i_ ;
virtual int foo() const = 0 ;
public:
XXX(){}
XXX( const XXX& x ) : i_( x.foo() ) {cout<<"hahha"<<i_<<endl ; }
};

class YY : public XXX {

int foo() const { return 13 ; }
public :
YY(){}
YY( const YY& y ) : XXX( y ) {}

};

int main( int argc, char* argv[] ){

YY y ;
YY x ( y ) ;
return 0 ;
}

Jul 19 '05 #4
"> > Is this a compiler bug. Or am I wrong somewhere.

Virtual functions are resolved statically if called from constructors
or destructors. Calling a pure virtual function from a constructor or
a destructor causes undefined behaviour.


But here the call was of a virtual function of *another* object that
has already been completely constructed elsewhere. ( IOW the virtual
function is not called on the object that is being constructed )
Are you saying that in general below code will fail to invoke foo()
polymorphically( undefined behavior ).

class X {
public:
X( some_type & y ) { y.foo() ; }
}

--sony
Jul 19 '05 #5
"Sony Antony" <so********@hotmail.com> wrote...
"> > Is this a compiler bug. Or am I wrong somewhere.

Virtual functions are resolved statically if called from constructors
or destructors. Calling a pure virtual function from a constructor or
a destructor causes undefined behaviour.
But here the call was of a virtual function of *another* object that
has already been completely constructed elsewhere. ( IOW the virtual
function is not called on the object that is being constructed )
Are you saying that in general below code will fail to invoke foo()
polymorphically( undefined behavior ).


No, in general it's impossible to tell. I can think of a couple
of ways to cause undefined behaviour with that.
class X {
public:
X( some_type & y ) { y.foo() ; }
What's 'some_type'?
}


Victor
Jul 19 '05 #6
Sony Antony <so********@hotmail.com> wrote in message
news:3e**************************@posting.google.c om...
I have the following simple program in Solaris Forte compiler 5.4
producing the warning. Though it produces the warning, it works fine
as expected.
This has been compiling fine without any warnings in the older 5.1
compiler. Since the latest compiler produces a warning, it makes me
suspecious about my own code. I still cannot find any problems with it
though.

It essentially produces a warning whenever a copy constructor of a
class with a pure virtual function invokes the pure virtual function
method on the instance that was passed to it.

Eg : -
class X {
public:
virtual void foo() = 0 ;
X (){}
X( X& x) { x.foo() ; }
}

Is this a compiler bug. Or am I wrong somewhere.
Thanks for the help


I think it's a compiler bug, caused by not distinguishing between the object
passed in and the object being constructed.

DW

Jul 19 '05 #7
"Victor Bazarov" <v.********@attAbi.com> wrote in message news:<LMC_a.95997$Oz4.22920@rwcrnsc54>...
"Sony Antony" <so********@hotmail.com> wrote...
"> > Is this a compiler bug. Or am I wrong somewhere.

Virtual functions are resolved statically if called from constructors
or destructors. Calling a pure virtual function from a constructor or
a destructor causes undefined behaviour.
But here the call was of a virtual function of *another* object that
has already been completely constructed elsewhere. ( IOW the virtual
function is not called on the object that is being constructed )
Are you saying that in general below code will fail to invoke foo()
polymorphically( undefined behavior ).


No, in general it's impossible to tell. I can think of a couple
of ways to cause undefined behaviour with that.


I dont understand. I have a completely constructed object ( of
arbitrary type some_type below ). My understanding is that no matter
what once the object has been constructed completely, it should act
polymorphically. It should act
polymorphic even if the method is invoked within another object's
constructor.

Like the case below y is a completely constructed object. When I call
its foo(), where is the undefined behavior. The fact that I m calling
it inside X's constructor should not matter at all right.

Now in the original case both X and some_type happened to be of the
same type.

--sony
class X {
public:
X( some_type & y ) { y.foo() ; }


What's 'some_type'?
}


Victor

Jul 19 '05 #8
"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:vj************@corp.supernews.com...
"Sony Antony" <so********@hotmail.com> wrote...
I dont understand. I have a completely constructed object ( of
arbitrary type some_type below ). My understanding is that no matter
what once the object has been constructed completely, it should act
polymorphically. It should act
polymorphic even if the method is invoked within another object's
constructor.


You are correct. However, the compiler does not know that when it
sees your constructor and a call to a function that it knows is pure
virtual. That's why it warns you.
Like the case below y is a completely constructed object. When I call
its foo(), where is the undefined behavior. The fact that I m calling
it inside X's constructor should not matter at all right.


That's why it's only a WARNING, not an error.


But it's an inappropriate and misleading warning isn't it? As the OP says,
there's no reason to believe that the object being copied is not a fully
constructed object. I suspect that if the code looked like this:

class X {
public:
virtual void foo() = 0 ;
X (){}
X( X& x) { foo() ; }
}

the compiler would also give a warning, not an error, even though it can
tell that this can't work. I think that's the warning it thinks it's giving
in the x.foo() case.
Now in the original case both X and some_type happened to be of the
same type.


Exactly. If they are, what prevents you from writing

X x(x);

?


Can 'x' be in any state to be copied when it reaches the copy constructor?
This is where the warning ought to be :)

DW

Jul 19 '05 #9
"David White" <no.email@provided> wrote...
"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:vj************@corp.supernews.com...
"Sony Antony" <so********@hotmail.com> wrote...
I dont understand. I have a completely constructed object ( of
arbitrary type some_type below ). My understanding is that no matter
what once the object has been constructed completely, it should act
polymorphically. It should act
polymorphic even if the method is invoked within another object's
constructor.
You are correct. However, the compiler does not know that when it
sees your constructor and a call to a function that it knows is pure
virtual. That's why it warns you.
Like the case below y is a completely constructed object. When I call
its foo(), where is the undefined behavior. The fact that I m calling
it inside X's constructor should not matter at all right.


That's why it's only a WARNING, not an error.


But it's an inappropriate and misleading warning isn't it? As the OP says,
there's no reason to believe that the object being copied is not a fully
constructed object. I suspect that if the code looked like this:

class X {
public:
virtual void foo() = 0 ;
X (){}
X( X& x) { foo() ; }
}

;
the compiler would also give a warning, not an error, even though it can
tell that this can't work.
It CAN'T tell. That's the point. You are allowed to have a body
of 'virtual void foo() = 0' elsewhere. If such body is provided,
the call would be OK. Try it:

struct X {
virtual void foo() = 0;
X() { foo(); }
};

#include <iostream>
void X::foo() {
std::cout << "Gotcha!\n";
}

struct Y : X {
void foo() {}
};

int main() {
Y y;
return 0;
}
I think that's the warning it thinks it's giving
in the x.foo() case.
Now in the original case both X and some_type happened to be of the
same type.
Exactly. If they are, what prevents you from writing

X x(x);

?


Can 'x' be in any state to be copied when it reaches the copy constructor?


Who said it was copying it? It's a parameterised constructor.
This is where the warning ought to be :)


Well, yes, here too. I actually think that the more warnings
the better.

Victor
Jul 19 '05 #10
Victor Bazarov <v.********@attAbi.com> wrote in message
news:vj************@corp.supernews.com...
"David White" <no.email@provided> wrote...
As the OP says,
there's no reason to believe that the object being copied is not a fully
constructed object. I suspect that if the code looked like this:

class X {
public:
virtual void foo() = 0 ;
X (){}
X( X& x) { foo() ; }
}

;

the compiler would also give a warning, not an error, even though it can
tell that this can't work.


It CAN'T tell. That's the point. You are allowed to have a body
of 'virtual void foo() = 0' elsewhere.


Oops. That's what I've telling other people recently. Okay, a warning is
appropriate in the above case, so forget that, but I still find it hard to
believe that the compiler warns in the x.foo() case because of the remote
possibility that x has not yet been constructed.

DW

Jul 19 '05 #11

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

Similar topics

20
by: Markus Sandheide | last post by:
Hello! Execute these lines: int x = 1; x = x > 2345678901; You will get: x == 1 with Borland C++ Builder
2
by: Simon Anders | last post by:
Hi, just a little question: I'm using the hash_set template from the GNU C++ standard library. Because the hashed containers are not standard content of the library but an extension taken...
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
6
by: Chad | last post by:
I'm not too sure if the question would fall under comp.lang.c or some kind of compiler newsgroup. I'm going to ask anyhow. Given the following: #include <stdio.h> int main(void) { int a =...
11
by: Henryk | last post by:
I have some very simple code fragment: short int n1, n2; .... n1 += n2; I get an warning from VS2003 - VS2005: warning C4244: '+=' : conversion from 'int' to 'short', possible loss of data ...
2
by: Robbie Hatley | last post by:
I'm getting a strange warning at work when I compile any file in our product that contains a deque of a particular struct. I don't understand this warning, so I'm not sure if this is a Microsoft...
24
by: asdf | last post by:
I got a warning from the following statement: fprintf(point_file, "CONTOUR\nCLOSE\n%d\n", curve.size()); warning: format '%d' expects type 'int', but argument 3 has type 'size_t' should I...
2
by: Chris Peters | last post by:
Hi, I'm using some Fortran 77 code together with C, and the compiler is giving me some strange warnings. Can anyone help explain what's going on? The code runs just fine. 1) Fortran code
1
by: raylopez99 | last post by:
The below did not set off my compiler, perhaps because I set the warnings to a higher (less nag) level. Strange. FYI, no question being asked. RL double d2030;
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.