question on class template virtual member function



 DEVELOP > c-Plus-Plus > question on class template virtual member function

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "India"
Date: 07 Nov 2007 05:22:45 AM
Object: question on class template virtual member function
#include <iostream>
using namespace std;
class Rec
{
public:
Rec(int arg = 10) : val(arg) { }
private:
int val;
};
template <class T> class Test
{
private:
T t;
public:
virtual void print( ) const { cout << t << endl; }
};
int main( )
{
Test<Rec> r;
return 0;
}
This program does not compile with g++ and also VC++2005 Express
Edition. However if the keyword 'virtual' is removed in
Test<T>::print(), it compiles fine. Since 'print()' function is not at
all called in this program, why do I get compilation error if
Test<T>::print() is virtual ?
Kindly explain.
Thanks
V.Subramanian
.

User: "Michael DOUBEZ"

Title: Re: question on class template virtual member function 07 Nov 2007 06:20:22 AM
subramanian100in@yahoo.com, India a écrit :

#include <iostream>

using namespace std;

class Rec
{
public:
Rec(int arg = 10) : val(arg) { }

private:
int val;
};

template <class T> class Test
{
private:
T t;

public:
virtual void print( ) const { cout << t << endl; }

};

int main( )
{
Test<Rec> r;

return 0;

}

This program does not compile with g++ and also VC++2005 Express
Edition. However if the keyword 'virtual' is removed in
Test<T>::print(), it compiles fine. Since 'print()' function is not at
all called in this program, why do I get compilation error if
Test<T>::print() is virtual ?

I have not checked in the standard but my intuition is that the virtual
function print() get instanciated in order to populate the vtable of
Test<Rec>.
When virtual is removed, the function is not instanciated because it is
not used.
When instanciated, the Test<Rec>::print() fails because
cout<<Rec()<<endl is not defined.
Michael
.
User: "India"

Title: Re: question on class template virtual member function 08 Nov 2007 01:16:46 AM
On Nov 7, 7:20 am, Michael DOUBEZ <michael.dou...@free.fr> wrote:

subramanian10...@yahoo.com, India a =E9crit :



#include <iostream>


using namespace std;


class Rec
{
public:
Rec(int arg =3D 10) : val(arg) { }


private:
int val;
};


template <class T> class Test
{
private:
T t;


public:
virtual void print( ) const { cout << t << endl; }


};


int main( )
{
Test<Rec> r;


return 0;


}


This program does not compile with g++ and also VC++2005 Express
Edition. However if the keyword 'virtual' is removed in
Test<T>::print(), it compiles fine. Since 'print()' function is not at
all called in this program, why do I get compilation error if
Test<T>::print() is virtual ?


I have not checked in the standard but my intuition is that the virtual
function print() get instanciated in order to populate the vtable of
Test<Rec>.

When virtual is removed, the function is not instanciated because it is
not used.

When instanciated, the Test<Rec>::print() fails because
cout<<Rec()<<endl is not defined.

Michael

How the vtable is created and what will be its contents ?
Does the standard specify anything regarding the above ?
Kindly explain.
Thanks
V=2ESubramanian
.
User: "Michael DOUBEZ"

Title: Re: question on class template virtual member function 08 Nov 2007 02:01:32 AM
subramanian100in@yahoo.com, India a écrit :

On Nov 7, 7:20 am, Michael DOUBEZ <michael.dou...@free.fr> wrote:

subramanian10...@yahoo.com, India a écrit :



#include <iostream>
using namespace std;
class Rec
{
public:
Rec(int arg = 10) : val(arg) { }
private:
int val;
};
template <class T> class Test
{
private:
T t;
public:
virtual void print( ) const { cout << t << endl; }
};
int main( )
{
Test<Rec> r;
return 0;
}
This program does not compile with g++ and also VC++2005 Express
Edition. However if the keyword 'virtual' is removed in
Test<T>::print(), it compiles fine. Since 'print()' function is not at
all called in this program, why do I get compilation error if
Test<T>::print() is virtual ?

I have not checked in the standard but my intuition is that the virtual
function print() get instanciated in order to populate the vtable of
Test<Rec>.

When virtual is removed, the function is not instanciated because it is
not used.

When instanciated, the Test<Rec>::print() fails because
cout<<Rec()<<endl is not defined.


How the vtable is created and what will be its contents ?
Does the standard specify anything regarding the above ?

Kindly explain.

From http://www.spec.org/cpu2006/Docs/447.dealII_CPPStds.txt
[...]When a class template is implicitly instantiated, each declaration
of its members is instantiated as well, but the corresponding
definitions are not. There are a few exceptions to this. First, if the
class template contains an anonymous union, the members of that union's
definition are also instantiated. There is also something tricky with
default functional call argument which I will not go into. The other
exception occurs with virtual member functions. Their definitions may
or may not be instantiated as a result of instantiating a class
template. Many implementations will, in fact, instantiate the definition
because the internal structure that enables the virtual call mechanism
requires the virtual functions actually to exist as linkable entities.[snip]
Paragraph 9 of 14.7.1, reads:
An implementation shall not implicitly instantiate a function
template, a member template, a non-virtual member function, a
member class or a static data member of a class template that
does not require instantiation. It is unspecified whether or
not an implementation implicitly instantiates a virtual member
function of a class template if the virtual member function
would not otherwise be instantiated. [...]
Michael
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER