如何在C ++中使用模板展开一个短循环?

编程入门 行业动态 更新时间:2024-10-27 11:17:51
本文介绍了如何在C ++中使用模板展开一个短循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不知道如何获得这样的东西

I wonder how to get something like this

1 write

copy(a,b,2,3)

2然后get,

a[2]=b[2]; a[3]=b[3]; a[4]=b[4];

我知道C #defines不能递归地使用所以我假设模板元程序已经就位。

I know that C #defines can't be used recursively to get that effect, so I suppose that template meta-programming is in place.

我知道有一个boost库,但我只想要简单技巧

I know there is a boost library for that, but I only want that "simple" trick, and boost is too "messy".

谢谢

推荐答案

最直接的解决方案是写一个循环,其中已知开始值和结束值。

The most straight forward solution to this is to write a loop where the start and end values are known

for(int i = 2; i <= 4; i++) { a[i]=b[i]; }

我认为这比任何类型的模板/运行时调用混合更好:编写的循环对编译器的优化器是完全清楚的,没有任何级别的函数调用,只是为了看看发生了什么。

I think this is better than any sort of template/runtime-call mixture: The loop as written is completely clear to the compilers' optimizer, and there are no levels of function calls to dig through just to see what's going on.

更多推荐

如何在C ++中使用模板展开一个短循环?

本文发布于:2023-11-26 12:26:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633926.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!