模板专业化,嵌套 [英] Template specialization, nested
问题描述
我使用模板化函数来处理ToString功能。例如:
模板< void ToString< int>(std :: string& s,int& value)
{
s = str(boost :: format("%1 $ d")%value);
}
template< void ToString< float>(std :: string& s,float& value)
{
s = str(boost :: format("%1 $ 1.7g") ;)%));
}
// ...更专业化......
现在我'' d想为std :: vector添加一个ToString版本。但是我真正想做的就是调用相应的ToString< element_typewhere
" element_type"是向量中元素的类型。是否可以使用单个ToString函数来处理这个问题,或者我是否必须为每种可能类型的向量元素写一个
?这似乎是一种浪费,因为每个函数中的代码都是相同的,除了里面的内容
< bracket。
>
这里有一种我想要的伪代码,但我不知道如何使用b $ b使语法有效...
//创建一个形式为[1,2,3,4,5]的字符串
模板<>
void ToString< std: :vector< element_type>>(std :: string& s,
std :: vector< element_type& value)
{
s =" [" ;;
std :: vector< element_type> :: iterator i = value.begin();
while (i!= value.end())
{
std :: string temp;
ToString< element_type>(temp,* i );
i ++;
s + = temp;
if(i!= value) .end())
{
s + ="," ;;
}
}
s + ="]" ;;
}
希望例子是有道理的。谢谢。
Pat
Hi,
I use templated functions to handle "ToString" functionality. For
example:
template<void ToString<int>(std::string &s, int &value)
{
s = str(boost::format("%1$d") % value);
}
template<void ToString<float>(std::string &s, float &value)
{
s = str(boost::format("%1$1.7g") % value);
}
// ... lots more specialization ...
Now I''d like to add a version of ToString for std::vector. But all I
really want to do is call the corresponding ToString<element_typewhere
"element_type" is the type of element in the vector. Is it possible to
handle this with a single ToString function, or do I have to write one
for every possible type of vector element? It seems like a waste, since
the code in every function would be identical except for what''s inside
the <brackets.
Here''s kind of a psuedo-code for what I want, but I don''t know how to
make the syntax work...
// Create a string of the form "[1, 2, 3, 4, 5]"
template<>
void ToString<std::vector<element_type>>(std::string &s,
std::vector<element_type&value)
{
s = "[";
std::vector<element_type>::iterator i = value.begin();
while (i != value.end())
{
std::string temp;
ToString<element_type>(temp, *i);
i++;
s += temp;
if (i != value.end())
{
s += ", ";
}
}
s += "]";
}
Hope the example makes sense. Thanks.
Pat
推荐答案
d")%value);
}
模板< void ToString< float>(std :: string& s,float& value)
{
s = str(boost :: format("%1
d") % value);
}
template<void ToString<float>(std::string &s, float &value)
{
s = str(boost::format("%1
1.7g")%value);
}
// ...更多专业化......
现在我想为std :: vector添加一个ToString版本。但是我真正想做的就是调用相应的ToString< element_typewhere
" element_type"是向量中元素的类型。是否可以使用单个ToString函数来处理这个问题,或者我是否必须为每种可能类型的向量元素写一个
?这似乎是一种浪费,因为每个函数中的代码都是相同的,除了里面的内容
< bracket。
>
这里有一种我想要的伪代码,但我不知道如何使用b $ b使语法有效...
//创建一个形式为[1,2,3,4,5]的字符串
模板<>
void ToString< std: :vector< element_type>>(std :: string& s,
std :: vector< element_type& value)
{
s =" [" ;;
std :: vector< element_type> :: iterator i = value.begin();
while (i!= value.end())
{
std :: string temp;
ToString< element_type>(temp,* i );
i ++;
s + = temp;
if(i!= value) .end())
{
s + ="," ;;
}
}
s + ="]" ;;
}
希望例子是有道理的。谢谢。
Pat
1.7g") % value);
}
// ... lots more specialization ...
Now I''d like to add a version of ToString for std::vector. But all I
really want to do is call the corresponding ToString<element_typewhere
"element_type" is the type of element in the vector. Is it possible to
handle this with a single ToString function, or do I have to write one
for every possible type of vector element? It seems like a waste, since
the code in every function would be identical except for what''s inside
the <brackets.
Here''s kind of a psuedo-code for what I want, but I don''t know how to
make the syntax work...
// Create a string of the form "[1, 2, 3, 4, 5]"
template<>
void ToString<std::vector<element_type>>(std::string &s,
std::vector<element_type&value)
{
s = "[";
std::vector<element_type>::iterator i = value.begin();
while (i != value.end())
{
std::string temp;
ToString<element_type>(temp, *i);
i++;
s += temp;
if (i != value.end())
{
s += ", ";
}
}
s += "]";
}
Hope the example makes sense. Thanks.
Pat
2008-05-24 13:20:30 -0400,你的名字< no * *@none.nonesaid:
On 2008-05-24 13:20:30 -0400, Your Name <no**@none.nonesaid:
>
//创建一个形式为[1,2,3,4]的字符串,5]"
模板<>
void ToString< std :: vector< element_type>>(std :: string& s,
std :: vector< element_type& value)
>
// Create a string of the form "[1, 2, 3, 4, 5]"
template<>
void ToString<std::vector<element_type>>(std::string &s,
std::vector<element_type&value)
好像你需要部分专业化:
模板< class element_type>
void ToString< std :: vector< element_type>>(std:string& s,
std :: vector< element_type& value)
-
Pete
Roundhouse Consulting,Ltd。( www.versatilecoding.com )
标准C ++库扩展:一个教程和参考的作者
( www.petebecker.com/tr1book )
Seems like you need a partial specialization:
template <class element_type>
void ToString<std::vector<element_type>>(std:string &s,
std::vector<element_type&value)
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
这篇关于模板专业化,嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!