是否有理由使用":: template"?

tem*_*boy 5 c++ templates c++11

template从全局命名空间获取模板名称时,可以使用关键字:

template <class T> void function_template();

template <class T>
void h()
{
    ::template function_template<T>();
}

int main() { h<int>(); }
Run Code Online (Sandbox Code Playgroud)

但是这段代码没有它就可以编译.人们可能想要这样做的情况是什么?

Who*_*aig 8

我可以想到一个地方,但我觉得这很常见:

#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
}
Run Code Online (Sandbox Code Playgroud)

产量

void function_template(int)
void function_template(int)
void function_template(T) [T = int]
Run Code Online (Sandbox Code Playgroud)

我打算在一个匿名命名空间中填充其中的一些,实际上带来了非平凡的意义,::但这似乎已经足够了,所以我把它排除在外.