是否可以从模板生成函数作为 f() 和 f<T>()?
Is it possible to have functions generated from template as f() and f<T>()?
是否可以将模板生成的函数为f()
和f<T>()
?
我想大部分时间都使用指定的类型来调用F,例如:
f<string>();
f<int>();
,但我还需要这样称呼:
f();
和未指定的类型应为字符串。这可能吗?
template <typename T>
void f() { ... }
void f() { f<string>(); }
您可以为模板参数提供默认类型:
template<class T=std::string>
foo()
注意:如果将默认参数转换为模板类,则必须用Foo<>
声明默认版本。调用模板函数时,这不是必需的;您可以在没有角度括号的情况下调用默认版本:foo()
另一个注意事项:由于模板参数扣除而起作用。标准的报价(2012年1月草案§14.8.2.5)强调矿山:
由此产生的替换和调整后的功能类型用作 模板参数扣除的函数模板类型。如果 尚未推导模板参数,其默认模板参数, 如果有的话,可以使用。[示例:
template <class T, class U = double>
void f(T t = 0, U u = 0);
void g() {
f(1, ’c’); //f<int,char>(1,’c’)
f(1); //f<int,double>(1,0)
f(); //error: T cannot be deduced
f<int>(); //f<int,double>(0,0)
f<int,char>(); //f<int,char>(0,0)
}
#include <string>
template <typename T=std::string>
void f() {}
int main()
{
f();
}
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中