class template member function - compilation error

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

consider the following program

#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:
void print( ) const { cout << t << endl; }
};

int main( )
{
Test<Rec> r;

// r.print( );

return 0;
}

This program compiles fine with g++ and VC++2005 Express Edition.

However if I remove the comment in the line
// r.print( ),
I get compilation error because operator<<( ) is not defined for Rec.

Why doesn't the compiler report this error when it tries to generate
(that is, instantiate) a class declaration for Test<Rec> itself?. Does
it mean the that the template member function definition is generated
only when the corresponding function is used(Test<T>::print( ) in this
case) ?

Kindly explain.

Thanks
V.Subramanian
 
N

Neelesh Bodas

consider the following program

#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:
void print( ) const { cout << t << endl; }

};

int main( )
{
Test<Rec> r;

// r.print( );

return 0;

}

This program compiles fine with g++ and VC++2005 Express Edition.

However if I remove the comment in the line
// r.print( ),
I get compilation error because operator<<( ) is not defined for Rec.

Why doesn't the compiler report this error when it tries to generate
(that is, instantiate) a class declaration for Test<Rec> itself?. Does
it mean the that the template member function definition is generated
only when the corresponding function is used(Test<T>::print( ) in this
case) ?

Yes, when the template member function is non-virtual.

14.7.1.(9) from the C++ standard: "An implementation shall not
implicitly instantiate a function template, a member template, a
nonvirtual member function, a member class or a static data member of
a class template that does not require instantiation"

-N
 
N

nick4ng

Yes, when the template member function is non-virtual.

14.7.1.(9) from the C++ standard: "An implementation shall not
implicitly instantiate a function template, a member template, a
nonvirtual member function, a member class or a static data member of
a class template that does not require instantiation"

-N

AFAIK it is not possible to define template virtual method. In general
as you have reported:
14.7.1.(9) from the C++ standard: "An implementation shall not
implicitly instantiate a function template,...".
This is a feature used for example in the policy pattern.

Cheers
n
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top