question on class template virtual member function

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

#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
 
M

Michael DOUBEZ

(e-mail address removed), 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
 
S

subramanian100in

(e-mail address removed), India a écrit :










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.Subramanian
 
M

Michael DOUBEZ

(e-mail address removed), India a écrit :
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
 

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,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top