c ++模板特化,使用typename时出错
c++ template specialization, error when using typename
我需要特化一个成员函数:
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)抱怨无法匹配任何模板声明:
error: template-id ‘writeVectorVariableUnit<>’ for ‘int32_t
writeVectorVariableUnit(foo&, __gnu_cxx::__normal_iterator > >&, __gnu_cxx::__normal_iterator > >&, const std::string&, const std::string&, const std::string&,
const std::vector,
std::allocator >, std::allocator, std::allocator > > >&) const’ does not
match any template declaration
我怀疑是跟typename的用法有关,试了好几种组合都没有成功。
你有什么建议吗?
这些问题与typename
的使用无关,它们是
您应该从 class 定义中专门化模板。
对于std::vector<uint16_t>::const_iterator
,T
不能推导为uint16_t
,因为这个属于non-deduced contexts。这意味着您必须明确指定专门的模板参数。
If a template parameter is used only in non-deduced contexts and is
not explicitly specified, template argument deduction fails.
1) The nested-name-specifier (everything to the left of the scope
resolution operator ::) of a type that was specified using a
qualified-id:
例如
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 {
// ...
}
我相信 There 实际上是在非推导上下文中,所以函数实际上不能推导 T (see this question)
你能做的最坏情况:
writeVectorVariableUnit<float>
我需要特化一个成员函数:
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)抱怨无法匹配任何模板声明:
error: template-id ‘writeVectorVariableUnit<>’ for ‘int32_t writeVectorVariableUnit(foo&, __gnu_cxx::__normal_iterator > >&, __gnu_cxx::__normal_iterator > >&, const std::string&, const std::string&, const std::string&, const std::vector, std::allocator >, std::allocator, std::allocator > > >&) const’ does not match any template declaration
我怀疑是跟typename的用法有关,试了好几种组合都没有成功。 你有什么建议吗?
这些问题与typename
的使用无关,它们是
您应该从 class 定义中专门化模板。
对于
std::vector<uint16_t>::const_iterator
,T
不能推导为uint16_t
,因为这个属于non-deduced contexts。这意味着您必须明确指定专门的模板参数。
If a template parameter is used only in non-deduced contexts and is not explicitly specified, template argument deduction fails.
1) The nested-name-specifier (everything to the left of the scope resolution operator ::) of a type that was specified using a qualified-id:
例如
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 {
// ...
}
我相信 There 实际上是在非推导上下文中,所以函数实际上不能推导 T (see this question)
你能做的最坏情况:
writeVectorVariableUnit<float>