【发布时间】:2020-09-01 13:44:13
【问题描述】:
所以我想做 2 个函数:一个用于数字(带模板),一个用于字符串。 这是我最好的尝试:
标题:
class myIO
{
public:
template<class Arithmetic,
class = enable_if_t< is_arithmetic_v <Arithmetic>>
>
static Arithmetic Input();
template<>
static string Input<string, void>();
};
cpp:
template<class Arithmetic, class>
static Arithmetic myIO::Input()
{
Arithmetic x;
//...
return x;
}
template<>
static string myIO::Input<string, void>()
{
string x;
//...
return x;
}
此实现有效,但如果我想将它与 string 一起使用,我必须使用 string x = myIO::Input<string, void>();
我希望能够只写<string> 而不是<string, void>
这可能吗?
【问题讨论】:
标签: c++ templates header specialization enable-if