如何使模板成员函数专用化 std::vector<T>

How can I specialize a template member function for std::vector<T>

本文关键字:vector gt lt std 何使模 成员 函数 专用      更新时间:2023-10-16

我需要以两种不同的方式定义get方法。一次用于简单类型t,一次用于std::vector。

template<typename T>
const T& Parameters::get(const std::string& key)
{
    Map::iterator i = params_.find(key);
    ...
    return boost::lexical_cast<T>(boost::get<std::string>(i->second));
    ...
}

如何对std::vector特化这个方法?在那里,代码应该看起来像这样:

template<typename T>
const T& Parameters::get(const std::string& key)
{
    Map::iterator i = params_.find(key);
    std::vector<std::string> temp = boost::get<std::vector<std::string> >(i->second)
    std::vector<T> ret(temp.size());
    for(int i=0; i<temp.size(); i++){
         ret[i]=boost::lexical_cast<T>(temp[i]);
    }
    return ret;    
}

但是我不知道如何专门化这个函数。非常感谢。

不要专门化函数模板。

  • 为什么不特化函数模板?
  • 模板专门化和重载

应该使用重载。

写一个函数模板get_impl来处理一般情况,并且重载(不是特殊化)来处理特殊情况,然后从get调用get_impl作为:

template<typename T>
const T& Parameters::get(const std::string& key)
{
     //read the explanation at the bottom for the second argument!
     return get_impl(key, static_cast<T*>(0) );
}

下面是实际的实现。

//general case
template<typename T>
const T& Parameters::get_impl(const std::string& key, T*)
{
    Map::iterator i = params_.find(key);
    return boost::lexical_cast<T>(boost::get<std::string>(i->second));
}
//this is overload - not specialization
template<typename T>
const std::vector<T>& Parameters::get_impl(const std::string& key, std::vector<T> *)
{
      //vector specific code
}

get中的static_cast<T*>(0)只是消除调用歧义的一种棘手的方法。static_cast<T*>(0)的类型是T*,将其作为第二个参数传递给get_impl将有助于编译器选择正确的get_impl版本。如果T不是std::vector,则选择第一个版本,否则选择第二个版本。

叫别的名字?例如

template<typename T>
const T& Parameters::getVector(const std::string& key)
{
  Map::iterator i = params_.find(key);
  std::vector<std::string> temp = boost::get<std::vector<std::string> >(i->second)
  // T is already a vector
  T ret; ret.reserve(temp.size());
  for(int i=0; i<temp.size(); i++){
     ret.push_back(boost::lexical_cast<typename T::value_type>(temp[i]));
  }
  return ret;  
}

你必须把它命名为:

foo.getVector<std::vector<int> > ("some_key");

你的问题没有排除这一点。

现在,如果您确实需要使用get(),那么您必须依赖部分专门化结构,因为该语言不支持函数的部分专门化。

这个要复杂得多,例如:

template <typename T>
struct getter
{
  const T& operator()(std::string const& key)
  {
    // default operations
  }
};
// Should double check this syntax 
template <typename T>
struct getter<std::vector<T, std::allocator<T> > >
{
  typedef std::vector<T, std::allocator<T> > VecT;
  const VecT& operator()(std::string const& key)
  {
    // operations for vector
  }
};

Then in you method变成:

template<typename T>
const T& Parameters::get(const std::string& key)
{
  return getter<T>()(key); // pass the structures getter needs?
}