jeudi 17 février 2022

Point of Instantiation for function templates

I am learning about templates in C++. In particular, i read about POIs. So i am trying out by reading and writing different examples. One such example which is not clear to me is given below:

template<typename T>
void g1(T p)
{

}
template<typename T>
void f1(T x)
{
g1(x); // #1: Is this point a POI for g1<T>(x)?
}
//#2: Or is this point a POI for g1<T>(x)?


//#3: Or is this point a POI for g1<T>(x)?
int main()
{
f1(7); // this point cannot be a POI 
}
// #4 I know that this point 4 is a  POI for f1<int>(int) but is this point also a POI for g<T>(x)?

In the above snippet, i know that point #4 is a POI for f1<int>(int). But is point #4 also a POI for g1<T>(x)? I know that in the statement g1(x); the name g1 is an unqualified dependent name. I also know that the POI

for a reference to a function template specialization to be immediately after the nearest namespace scope declaration or definition that contains that reference.

This is why point #4 is a POI for f<int>(int). But then similarly, point #2 should be a POI for g1(x);. But i am not sure where is the POI for g1(x);. So what are the rules for finding the POI for an unqualified dependent name like g1.

My questions are:

  1. Is point #4 also POI for g1<T>(x)? If yes then what are the rules for finding the POI for an unqualified dependent name like g1.

  2. According to the statement i quoted(which is from C++ Templates: The Complete Guide), shouldn't point #2 be a POI for g1<T>(x);. I guess this boils down to whether the statement g1(x); refers to a function template specialization. If yes, then according to the statement quoted, point #2 should be a POI for g<T>(x);. I might be wrong here.

  3. What will change if i remove the statement f1(7);. To my understanding there will be no POI in this translation unit anymore. But i am not 100% sure of this. Or removing f1(7); will make the program ill-formed now.

Aucun commentaire:

Enregistrer un commentaire