mardi 15 août 2023

Local function template in cpp file: should I make it static?

// CPP file

template<typename T>
void foo()
{
}

// Use the template only in this CPP as foo<int>, foo<char>, etc.

Suppose I have CPP file and there I have a template function foo for internal usage inside this CPP file only. Do I understand right that if I don't put the template into anonymous namespace or don't make it static, its instantiations used/created in this CPP file (e.g. foo<int>{}, foo<char>{}, etc.) will have outer linkage, i.e. will be seen outside. So is it true, that I better have the template static, as shown below or have it in anonymous namespace?

// CPP file

template<typename T>
static void foo()
{
}

Aucun commentaire:

Enregistrer un commentaire