如何专门化std :: vector< T>的模板成员函数 [英] How can I specialize a template member function for std::vector<T>
问题描述
我需要以两种不同的方式定义一个get方法。一个用于简单类型T.一个用于std :: vector。
template< typename T&
const T&参数:: get(const std :: string& key)
{
Map :: iterator i = params_.find(key);
...
return boost :: lexical_cast< T>(boost :: get< std :: string>(i-> second));
...
}
如何专门化std的这种方法: :向量。因为代码应该看起来像这样:
template< typename T&
const T&参数:: 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 ret [i] = boost :: lexical_cast< T>(temp [i]);
}
return ret;
}
但我不知道如何专门化这个函数。非常感谢。
不要专业化功能模板 b
写一个函数模板 get_impl
来处理一般情况,并且 overload (不是 specialize )来处理特定情况,然后调用 get_impl
从
get
as:
template< typename T> ;
const T&参数:: get(const std :: string& key)
{
//读取第二个参数底部的说明!
return get_impl(key,static_cast< T *>(0));
}
这里是实际的实现。
//一般情况
模板< typename T>
const T&参数:: get_impl(const std :: string& key,T *)
{
Map :: iterator i = params_.find(key);
return boost :: lexical_cast< T>(boost :: get< std :: string>(i-> second));
}
//这是重载 - 不特殊化
template< typename T>
const std :: vector< T>参数:: get_impl(const std :: string& key,std :: vector< T> *)
{
// vector specific code
}
中的
只是一个很难的方法来消除调用。 static_cast< T *>(0)
get static_cast< T *>(0)
的类型是 T *
,并将其作为第二个参数传递给 get_impl
将帮助编译器选择正确版本的 get_impl
。如果 T
不是 std :: vector
,将选择第一个版本,否则将选择第二个版本。
I need to define a get method in two different ways. One for simple types T. And once for 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));
...
}
How can I specialize this method for std::vector. As there the code should look something like this:
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;
}
But I do not know how to specialize the function for this. Thanks a lot.
Don't specialize function template.
Instead, use overload.
Write a function template get_impl
to handle the general case, and overload (not specialize) this to handle the specific case, then call get_impl
from get
as:
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) );
}
And here goes the actual implementations.
//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
}
The static_cast<T*>(0)
in get
is just a tricky way to disambiguate the call. The type of static_cast<T*>(0)
is T*
, and passing it as second argument to get_impl
will help compiler to choose the correct version of get_impl
. If T
is not std::vector
, the first version will be chosen, otherwise the second version will be chosen.
这篇关于如何专门化std :: vector< T>的模板成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!