You can use the template
keyword when getting a template name from a global namespace:
template <class T> void function_template();
template <class T>
void h()
{
::template function_template<T>();
}
int main() { h<int>(); }
But this code can compile without it. What are the situations in which one might want to do this?
I can think of one place, but I hardly think it would be common:
#include <iostream>
// simpile function template
template<class T>
void function_template(T)
{
std::cout << __PRETTY_FUNCTION__ << '\n';
}
// overload (NOT specialized)
void function_template(int value)
{
std::cout << __PRETTY_FUNCTION__ << '\n';
}
int main()
{
function_template(0); // calls overload
::function_template(0); // calls overload
::template function_template(0); // calls template, deduces T
}
Output
void function_template(int)
void function_template(int)
void function_template(T) [T = int]
I was going to stuff some of this in an anonymous namespace to actually bring non-trivial meaning to ::
but this seems to be sufficient so I left it out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With