如何使模板成员函数专用化 std::vector<T>
How can I specialize a template member function for std::vector<T>
我需要以两种不同的方式定义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?
}
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中用vector填充一个简单的动态数组
- vector.resize()中的分配错误
- 使用std::vector的OpenCL矩阵乘法
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 在某些循环内使用vector.push_back时出现分段错误
- 当vector是tje全局变量时,c++中vector的内存管理
- std::vector的包装器,使数组的结构看起来像结构的数组
- 为什么(-1)%vector::size()总是返回0
- 在C++中将类(带有Vector成员)保存为二进制文件
- 编译器如何区分std::vector的构造函数
- 将 int 数组转换为 std::vector<int*>
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- EASTL矢量<向量<int>>连续的
- 在std::vector上存储带有模板的类实例
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 使用 std::vector<boost::shared_ptr<Base_Class>> 或 boost::p tr_vector 的性能注意事项是什么<Base>
- 将矩阵"shared_ptr<vector<vector>sp"的行"(*sp)[i]"<T>传递给接受"shared_ptr<vector<T>>
- 如何获取std::vector<DMatch>