Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ template doubts.

Status
Not open for further replies.

isaisa

Programmer
May 14, 2002
97
IN
Hi all,
Following are some doubts regarding templates

1. When i say that the template is instatiating, i need the concrete type to instantiate the template. i am not clear on this as to what types i refer to be concrete. Extending this when i have the following code
Code:
   //File.h
   template <typename T>
     void foo(T temp) {
        // some code here 
     }
   
   //FileMain.cpp
   int i = 40;
   int *ptr = &i;
   foo(ptr);
In the code snippet above, does the statement means that the pointer type is also the concrete type ????

2. it is said that the template functions have external linkage normally. what is the reason behind this ?


thanks in advance

sanjay
 
hi all
wnated add some more doubts in the last post as follows

3. while overloading the template function, the template parameters are not taken in to consideration as per my understanding. what is the reason behind this ? The difference in the template parameters represents the different set of function families. Am i correct on thinking ?


4. Function temeplates and the template classes internally generates the version for after specialization happens or rather instantiation happens for specific data types. In case of class templates, how class with the same names but for different data types are maintained in the system? [assuming the instantiation happens in the same translation unit in the same namespace scope].

Regards
sanjay
 
1.
C++ Standard said:
There is no semantic difference between class and typename in a template-parameter.
But class and typename keywords are not aliases in other contexts!
T is a type in your snippet: int, int*, int&, your classes etc are types. Template parametes are types or non-types (for example, int i).

2. Normally, as usually - that's all. You may define template function with internal linkage, of course (with static keyword - but what for?). See also:
C++ Standard 1.4 Implementation compliance. The templates said:
It's about C++ Standard Library templates...
 
1. A concrete type is a non-template, i.e. something that you can create as a variable like: int num;

The following would not compile: vector<list> blah; because list is also a template and needs a type specified. So to turn the list into a concrete type, so that vector also has a concrete type, you can do this: vector<list<int> > blah;

2. I have no idea.

3. Could you give an example? I'm not exactly sure what you're asking there.

4. Class templates do have different names, because you have to specify a type along with it. Ex.
Code:
vector<int>  intArray;
vector<char> charArray;
 
hi
Thank all for clarifying my doubts.

cpjust, i got the answer in your question for point no 3. My question was wrong ...thanks for the help anyway ....


Sanjay

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top