模板函数作为函数参数 [英] template function as function parameter

查看:81
本文介绍了模板函数作为函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想知道是否有人处理过类似的问题。我需要使用

模板函数作为特定函数的参数(也是模板

函数)。

程序编译成一个对象文件,但在最后阶段,它说

,它找不到模板功能。该平台是WindowsXP Pro,MSCV ++

..Net。

更具体地说,我想做的是写一个atl算法,这也将是

接受另一个算法作为函数参数。这是

外部算法的接口:


模板<类迭代器,类outiterator,类Alg>

void transform_slide (迭代器开始,迭代器结束,outiterator

outbegin,std :: size_t slide,Alg alg)

{

std :: size_t loop = std :: distance(开始,结束)/ slide;

for(std :: size_t x = 0; x< loop; ++ x,++ outbegin,begin =

std :: advance(开始,大小))//只做完整周​​期,而不是部分

* outbegin = alg(开始,std :: advance(开始,大小));

}


我想要做的是在滑动

的基础上对范围执行一些计算。例如,如果我有1000个值的数组,我可能想要
计算每100个值的平均值。


任何帮助都将是赞赏。非常感谢提前。


RG

Hi all,

I was wondering if anyone had dealt with a similar problem. I need to use a
template function as the parameter for a particular function (also template
function).
The program compiles into an object file but then at the final stage it says
that it can''t find template function. The platform is WindowsXP Pro, MSCV++
..Net.

More specifically what I want to do is write an atl algorithm that will also
accept another algorithm as a function parameter. This is inteface of the
outer algorithm:

template <class iterator, class outiterator, class Alg>
void transform_slide ( iterator begin, iterator end, outiterator
outbegin, std::size_t slide, Alg alg)
{
std::size_t loop = std::distance( begin, end) / slide;
for ( std::size_t x = 0; x < loop; ++x, ++outbegin, begin =
std::advance( begin, size) ) // only do complete cycles, not parts
*outbegin = alg( begin, std::advance( begin, size) );
}

What I want to do with is perform some calculations on range on a sliding
basis. For example, if I have a array of 1000 values, I may want to
calculate the average of every 100 of those values.

Any help would be appreciated. Many Thanks in advance.

RG

推荐答案



" rg" ; < RG **** @ hotmail.com>在消息中写道

news:ce ********** @ news.freedom2surf.net ...

"rg" <rg****@hotmail.com> wrote in message
news:ce**********@news.freedom2surf.net...
大家好,
a模板函数作为特定函数的参数(也是
模板函数)。
程序编译成目标文件但在最后阶段它是
表示它找不到模板功能。该平台是WindowsXP Pro,
MSCV ++ .Net。


您是否陷入了将模板代码放入

实现(.cpp)文件而不是将其全部放在标题中的陷阱?请参阅

常见问题

http://www.parashift.com/c++-faq-lit...templates.html

有关链接器错误的信息模板。

更具体地说,我想要做的是写一个atl算法,
也接受另一个算法作为函数参数。这是
外部算法的接口:

模板<类迭代器,类outiterator,类Alg>
void transform_slide(迭代器开头,迭代器结束,outiterator
outbegin,std :: size_t slide,Alg alg)
{
std :: size_t loop = std :: distance(begin,end)/ slide;
for(std :: size_t x = 0; x< loop; ++ x,++ outbegin,begin =
std :: advance(begin,size))//只做完整的循环,而不是部分
* outbegin = alg(开始,std :: advance(开始,大小));
}
Hi all,

I was wondering if anyone had dealt with a similar problem. I need to use a template function as the parameter for a particular function (also template function).
The program compiles into an object file but then at the final stage it says that it can''t find template function. The platform is WindowsXP Pro, MSCV++ .Net.
Are you falling into the trap where you place template code in the
implementation (.cpp) file instead of placing all of it in the header? See
the FAQ
(http://www.parashift.com/c++-faq-lit...templates.html) for
info about linker errors with templates.
More specifically what I want to do is write an atl algorithm that will also accept another algorithm as a function parameter. This is inteface of the
outer algorithm:

template <class iterator, class outiterator, class Alg>
void transform_slide ( iterator begin, iterator end, outiterator
outbegin, std::size_t slide, Alg alg)
{
std::size_t loop = std::distance( begin, end) / slide;
for ( std::size_t x = 0; x < loop; ++x, ++outbegin, begin =
std::advance( begin, size) ) // only do complete cycles, not parts
*outbegin = alg( begin, std::advance( begin, size) );
}




这可能会对某些输入产生奇怪的行为。如果

距离为1且幻灯片为2且未执行任何操作,请考虑此行为。另外,你可以通过调用

advance()来意外地从容器的末端运行
。也许这样的事情会更安全:


模板<类iterator,类outiterator,类Alg>

void transform_slide(iterator begin,iterator end,outiterator

outbegin,std :: size_t slide,Alg alg)

{

std :: size_t dist = std :: distance(开始,结束) );

for(std :: size_t x = 0; x< dist; x + = slide,++ outbegin)

//只做完整周​​期,而不是部分

iterator tmp = std :: advance(开始,std :: min(幻灯片,dist-x));

* outbegin = alg(开始,tmp);

begin = tmp;

}


上面的代码未经测试。


-

David Hilsee



This may have weird behavior on some inputs. Consider the behavior if the
distance is 1 and the slide is 2, where nothing is performed. Also, you
could accidentally run off the end of the container with the call to
advance(). Maybe something like this would be safer:

template <class iterator, class outiterator, class Alg>
void transform_slide ( iterator begin, iterator end, outiterator
outbegin, std::size_t slide, Alg alg)
{
std::size_t dist = std::distance( begin, end );
for ( std::size_t x = 0; x < dist; x += slide, ++outbegin )
// only do complete cycles, not parts
iterator tmp = std::advance( begin, std::min(slide, dist-x) );
*outbegin = alg( begin, tmp );
begin = tmp;
}

The code above is untested.

--
David Hilsee


" David Hilsee" <哒************* @ yahoo.com>在消息中写道

新闻:-8 ******************** @ comcast.com ...
"David Hilsee" <da*************@yahoo.com> wrote in message
news:-8********************@comcast.com...
这可能会对某些输入产生奇怪的行为。如果
距离为1且幻灯片为2,则执行任何操作时,请考虑此行为。此外,您可能会通过调用
advance()意外地跑出容器的末端。也许这样的事情会更安全:
This may have weird behavior on some inputs. Consider the behavior if the
distance is 1 and the slide is 2, where nothing is performed. Also, you
could accidentally run off the end of the container with the call to
advance(). Maybe something like this would be safer:




对不起,关于集装箱末端AFAICT的评论不适用于你的
码。如果删除了对

std :: min的调用,它适用于我提供的代码。


-

David Hilsee



Sorry, that comment about the end of the container, AFAICT, does not apply
to your code. It applies to the code that I provided if the call to
std::min is removed.

--
David Hilsee


" David Hilsee" <哒************* @ yahoo.com>在消息中写道

新闻:-8 ******************** @ comcast.com ...
"David Hilsee" <da*************@yahoo.com> wrote in message
news:-8********************@comcast.com...
模板<类迭代器,类outiterator,类Alg>
void transform_slide(迭代器开头,迭代器结束,outiterator
outbegin,std :: size_t slide,Alg alg)
{
std :: size_t dist = std :: distance(开始,结束);
for(std :: size_t x = 0; x< dist; x + = slide,++ outbegin)
//只完成循环,而不是部分
iterator tmp = std :: advance(开始,std :: min(幻灯片,dist-x));
* outbegin = alg(开始,tmp );
begin = tmp;
}
template <class iterator, class outiterator, class Alg>
void transform_slide ( iterator begin, iterator end, outiterator
outbegin, std::size_t slide, Alg alg)
{
std::size_t dist = std::distance( begin, end );
for ( std::size_t x = 0; x < dist; x += slide, ++outbegin )
// only do complete cycles, not parts
iterator tmp = std::advance( begin, std::min(slide, dist-x) );
*outbegin = alg( begin, tmp );
begin = tmp;
}




对于我对自己的最终回应,我想指出我没有''' t $ / b $ b甚至试图理解我从原始的

代码中剪切和粘贴的评论,并使我发布的代码几乎完全无关紧要。 *很棒

额头*


-

David Hilsee



And for my final response to myself, I would like to point out that I didn''t
even try to understand the comment that I cut-and-pasted from the original
code and made the code I posted almost completely irrelevant. *smacks
forehead*

--
David Hilsee


这篇关于模板函数作为函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆