2

I need to specialized a member function:

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

in this way:

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;

but the g++ compiler (version 4.4.7 on a redhat 6.7) complains that is not able to match any template declaration:

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

I suspect that is related with the usage of typename, I try several combination without success. Do you have any suggestion?

songyuanyao
  • 169,198
  • 16
  • 310
  • 405

2 Answers2

4

The issues have nothing to do with the usage of typename, they are

  1. You should specialize the template out of the class definition.

  2. For std::vector<uint16_t>::const_iterator, T can't be deduced as uint16_t because this belongs to non-deduced contexts. That means you have to specify the specialized template parameter explicitly.

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:

e.g.

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 {
    // ...
}
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
0

I beleive T here is actually in a non deduced context, so the function cannot actually deduce T (see this question)

worst case you could do:

writeVectorVariableUnit<float>

live demo

Community
  • 1
  • 1
Biggy Smith
  • 910
  • 7
  • 14