有理由使用"::template"吗?
Is there ever a reason to use "::template"?
从全局命名空间获取模板名称时,可以使用 template
关键字:
template <class T> void function_template();
template <class T>
void h()
{
::template function_template<T>();
}
int main() { h<int>(); }
但是这段代码可以在没有它的情况下进行编译。在什么情况下可能想要这样做?
我可以想到一个地方,但我几乎不认为它会很常见:
#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
}
输出
void function_template(int)
void function_template(int)
void function_template(T) [T = int]
我打算将其中一些塞进一个匿名命名空间中,以实际为::
带来非平凡的意义,但这似乎已经足够了,所以我省略了它。
相关文章:
- 是否有理由在标题中保留完全专用的模板?
- 如果我有很多具有相似前缀的字符串,是否有理由从该前缀创建一个子字符串?
- 如果代码中没有连接任何插槽,是否有理由发出Qt信号?
- 是否有理由对非负常量使用无符号类型?
- 是否有理由大多数/所有 try-catch 示例只对 throw 语句使用 void 子函数
- 是否仍然有理由在C++代码中使用"int"
- 是否有理由使用 malloc 初始化构造函数中的指针
- 是否有理由跳过for循环的初始化
- C++11:是否有理由为什么某些常规类型不应该专门使用“std::hash”
- 是否有理由不使用单元强制类型
- C++:是否有理由使用uint64_t而不是size_t
- 是否有理由使用字符串 => 索引到向量的映射,而不是字符串 => 对象?
- 有理由使用"::template"吗?
- 在这段代码中,是否有理由对字符串文字使用const_cast
- 是否有理由无法默认构造具有空捕获列表的 lambda?
- 是否有理由不从可执行文件中删除符号?
- 是否有理由显式声明模板类型
- 是否有理由在错误处理的代码中不广泛看到错误?
- 使用元组和boost.融合,有理由使用结构吗
- 是否有理由选择一个变量的多个unordered_map而不是结构体的多个unordered_map ?