This question already has answers here:
explicit specialization of template class member function
(2个答案)
3年前关闭。
我需要专门的成员函数:
通过这种方式:
但是g++编译器(redhat 6.7上的版本4.4.7)抱怨无法匹配任何模板声明:
我怀疑与typename的使用有关,我尝试了几种组合,但均未成功。
你有什么建议吗? 您应该在类定义之外专门化模板。 对于
例如
(2个答案)
3年前关闭。
我需要专门的成员函数:
template <typename T>
int32_t writeVectorVariableUnit(
foo& classFoo,
typename std::vector<T>::const_iterator& start,
typename std::vector<T>::const_iterator& stop,
const std::string& name,
const std::string& unit,
const std::string& longName,
const std::vector<std::string>& dimName) const
通过这种方式:
template <>
int32_t writeVectorVariableUnit(
foo& classFoo,
std::vector<uint16_t>::const_iterator& start,
std::vector<uint16_t>::const_iterator& stop,
const std::string& name,
const std::string& unit,
const std::string& longName,
const std::vector<std::string>& dimName) const;
但是g++编译器(redhat 6.7上的版本4.4.7)抱怨无法匹配任何模板声明:
我怀疑与typename的使用有关,我尝试了几种组合,但均未成功。
你有什么建议吗?
最佳答案
这些问题与typename
的使用无关,它们是
std::vector<uint16_t>::const_iterator
,无法将T
推导为uint16_t
,因为它属于non-deduced contexts。这意味着您必须显式指定专用模板参数。 例如
template <>
int32_t bar::writeVectorVariableUnit<uint16_t> (
// ~~~~~ ~~~~~~~~~~
foo& classFoo,
std::vector<uint16_t>::const_iterator& start,
std::vector<uint16_t>::const_iterator& stop,
const std::string& name,
const std::string& unit,
const std::string& longName,
const std::vector<std::string>& dimName) const {
// ...
}
关于c++ - c++模板特化,使用类型名时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41938104/