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

class template member function - compilation error

consider the following program

#include <iostream>

using namespace std;

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

private:
int val;
};

template <class Tclass Test
{
private:
T t;

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

int main( )
{
Test<Recr;

// 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<Recitself?. 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

Nov 2 '07 #1
2 1827
On Nov 2, 11:33 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
consider the following program

#include <iostream>

using namespace std;

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

private:
int val;

};

template <class Tclass Test
{
private:
T t;

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

};

int main( )
{
Test<Recr;

// 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<Recitself?. 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

Nov 2 '07 #2
On 2 Nov, 08:57, Neelesh Bodas <neelesh.bo...@gmail.comwrote:
On Nov 2, 11:33 am, "subramanian10...@yahoo.com, India"

<subramanian10...@yahoo.comwrote:
consider the following program
#include <iostream>
using namespace std;
class Rec
{
public:
Rec(int arg = 10) : val(arg) { }
private:
int val;
};
template <class Tclass Test
{
private:
T t;
public:
void print( ) const { cout << t << endl; }
};
int main( )
{
Test<Recr;
// 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<Recitself?. 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
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

Nov 2 '07 #3

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

Similar topics

1
by: Lou | last post by:
I'm using the Response Filter Class in MS KB article 811162 (http://support.microsoft.com/default.aspx?scid=kb;EN-US;811162) to generate a static htm page from an asp.net template page being...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
9
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a...
4
by: zfareed | last post by:
#include <iostream> #include <fstream> using namespace std; template<class ItemType> class SortedList { private:
8
by: nishit.gupta | last post by:
I was having a problem with template class memer function definition , so i serched the net and find that template class member fuction definition should be in header file else compilation will...
1
by: scripty | last post by:
Hello, I am trying to implement hash table functionality using templates. I need to declare the hash table element as a struct or class inside the HTable template... I get...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
12
by: Angus | last post by:
I am writing a class as a wrapper around a std::map. The class is: template<class TKey, class TValue> class CGeneralMap : protected map<TKey, TValue> At the moment I have basic functions like...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.