有一种方法来定义两个不同类型的变量在一个for循环初始值设定器? [英] Is there a way to define variables of two different types in a for loop initializer?

查看:164
本文介绍了有一种方法来定义两个不同类型的变量在一个for循环初始值设定器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以在for循环中定义相同类型的2个变量:

You can define 2 variables of the same type in a for loop:

int main() {
  for (int i = 0, j = 0; i < 10; i += 1, j = 2*i) {
    cout << j << endl;
  }
}

但是定义不同类型的变量是非法的:

But it is illegal to define variables of different types:

int main() {
  for (int i = 0, float j = 0.0; i < 10; i += 1, j = 2*i) {
    cout << j << endl;
  }
}

有办法吗? (我不需要在循环中使用 i ,只需 j 。)

Is there a way to do this? (I don't need to use i inside the loop, just j.)

如果你有完全黑客和晦涩的解决方案,我可以。

If you have totally hacked and obscure solution, It's OK for me.

在这个例子中,我知道你可以使用 double 我正在寻找一个一般的答案。

In this contrived example I know you could just use double for both variables. I'm looking for a general answer.

请不要建议将任何变量移出体外,可能不适用于我,因为一个是迭代器必须在循环之后消失,并且for语句将被包含在我的 foreach 宏中:

Please do not suggest to move any of the variables outside of for body, probably not usable for me as one is an iterator that has to disappear just after the loop and the for statement is to be enclosed in my foreach macro:

#define foreach(var, iter, instr) {                  \
    typeof(iter) var##IT = iter;                     \
    typeof(iter)::Element var = *var##IT;            \
    for (; var##_iterIT.is_still_ok(); ++var##IT, var = *var#IT) {  \
      instr;                                         \
    }                                                \
  }

/ p>

It can be used thus:

foreach(ii, collection, {
  cout << ii;
}). 

但我需要一些像这样的东西:

But I need something that will be used like that:

foreach(ii, collection)
  cout << ii;

请不要引入任何运行时开销(但编译速度可能较慢)。

Please do not introduce any runtime overhead (but it might be slow to compile).

推荐答案

这里是一个使用boost预处理器的版本(这只是为了好玩,对于现实世界的答案, ):

Here is a version using boost preprocessor (This is just for fun. For the real-world answer, see @kitchen's one above):

FOR((int i = 0)(int j = 0.0), i < 10, (i += 1, j = 2 * i)) { 

}

第一部分指定一系列声明:(a)(b)... 。稍后声明的变量可以引用在它们之前声明的变量。第二部分和第三部分如往常一样。在逗号出现在第二和第三部分中,括号可以用来防止它们分离宏参数。

The first part specifies a sequence of declarations: (a)(b).... The variables declared later can refer to variables declared before them. The second and third part are as usual. Where commas occur in the second and third parts, parentheses can be used to prevent them to separate macro arguments.

我知道有两个技巧用于声明变量,这些变量以后在宏之外添加的复合语句中可见。第一个使用条件,如if:

There are two tricks known to me used to declare variables that are later visible in a compound statement added outside a macro. The first uses conditions, like an if:

if(int k = 0) ; else COMPOUND_STATEMENT

然后 k 当然,它总是必须评估 false 。所以它不能被我们使用。其他上下文是这一个:

Then k is visible. Naturally, it always have to evaluate to false. So it can't be used by us. The other context is this one:

for(int k = 0; ...; ...) COMPOUND_STATEMENT

这就是我要在这里使用。我们必须观察,只做一次迭代 COMPOUND_STATEMENT 。增量和条件检查的实际循环必须在结束,所以附加的复合语句适用于它。

That's what i'm going to use here. We'll have to watch to only make one iteration of COMPOUND_STATEMENT. The actual for loop that does the increment and condition checking has to come at the end, so the appended compound statement appertains to it.

#include <boost/preprocessor.hpp>
#include <iostream>

#define EMIT_DEC_(R,D,DEC) \
    for(DEC; !_k; ) 

#define FOR(DECS, COND, INC) \
    if(bool _k = false) ; else \
      BOOST_PP_SEQ_FOR_EACH(EMIT_DEC_, DECS, DECS) \
        for(_k = true; COND; INC)

int main() {
    FOR((int i = 0)(float j = 0.0f), i < 10, (i += 1, j = 2 * i)) {
        std::cout << j << std::endl;
    }
}

它创建了一堆 for 语句,每个嵌套到另一个语句中。它扩展为:

It's creating a bunch of for statements, each nested into another one. It expands into:

if(bool _k = false) ; else
  for(int i = 0; !_k; )
    for(float j = 0.0f; !_k; )
      for(_k = true; i < 10; (i += 1, j = 2 * i)) {
        std::cout << j << std::endl;
      }

这篇关于有一种方法来定义两个不同类型的变量在一个for循环初始值设定器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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