我有一个界面
std::string
get_string(Source const &s, std::string const &d);
int
get_int(Source const &s, int const &d);
bool
get_bool(Source const &s, bool const &d);
我想改为
template<class T>
T
get(Source const &s, T const &d);
But 没有合理的基本模板,所以实际的基本定义是合法但无用的(return d;
)。如果实例化了基础,我该怎么做才能强制编译时失败?对于这种情况有惯用的解决方案吗?
不要定义模板,只需声明它并定义三个专业化。
template <typename T>
T get(Source const &, T const &);
template<>
std::string get(Source const &s, std::string const &d) {
return d + s.stringval(); // or whatever
}
[编辑:删除了有关重载的内容 - 仅此一次,模板函数专业化实际上似乎更好。谁会敲门?]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)