【发布时间】:2011-09-29 09:30:09
【问题描述】:
我有一个模板方法,它有两个专用版本,用于类型 bool 和 vector<string>。
基础版本:
template <class T>
const T InternGetValue(const std::string& pfad) const
{
...
}
特殊版本:
template <>
const bool InternGetValue(const std::string& pfad) const
{
...
}
template <>
const std::vector<std::string> InternGetValue< std::vector<std::string>>(const std::string& pfad) const
{
...
}
现在我想实现 one 专业化,它将接受所有类型的 vector<aritmethic_data_type>,例如 vector<double> vector<int> 或 vector<float>。
我可以通过为上述类型编写重载来实现这一点,但我有兴趣通过另一个专业来实现我的目标。
这是我迄今为止尝试过的(导致错误“非法使用显式模板参数”):
template <class T>
const std::vector<T> InternGetValue< std::vector<T>>(const std::string& pfad, typename boost::enable_if<boost::is_arithmetic<T>>::type* dummy = 0) const
{
}
【问题讨论】:
-
完全专业化的功能模板很少有用。重载方法有什么问题?
-
@TomalakGeret'kal:重载方法当然没有错。我只是好奇如何通过专业化解决它。拓宽我的视野...... :-)
-
很公平。只要你掌握了所有的事实。 :)