2变量“嵌套”用TMP循环 [英] 2 variable "nested" loop with TMP

查看:73
本文介绍了2变量“嵌套”用TMP循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试做的是使用模板

元编程迭代两个变量。我已经专门做了这样的事情,当它到达终点时

的行开始于下一行,当它到达最后一行时它会停止..

至少这就是我以为我做的,但VC71说警告C4717:

''LOOP< 0,1> :: DO'':在所有控制路径上递归,函数将导致

运行时堆栈溢出"。

有什么问题?


这里是代码:

模板< int M,int N>

class LOOP {

private:

template< int I,int J>

类INNER {

public:

static inline void DO(){

cout<< ("<<<<","<< J<")" ;;

LOOP< I + 1,J> :: DO();

}

};

模板< int J>

类INNER< M ,J> {

public:

static inline void DO(){

LOOP< 0,J + 1> :: DO();

}

};

模板< int I>

类INNER< I,N> {

public:

static inline void DO(){

}

};

public:

static inline void DO(){

INNER< 0,0> :: DO();

}

};

What I try to do is to iterate over two variables using template
metaprogramming. I''ve specialized it such that when it reaches the end
of a row ot starts on the next and when it reaches the last row it stops..
At least that''s what I thought I did, but VC71 says "warning C4717:
''LOOP<0,1>::DO'' : recursive on all control paths, function will cause
runtime stack overflow".
What''s wrong?

Here''s the code:
template<int M, int N>
class LOOP {
private:
template<int I, int J>
class INNER {
public:
static inline void DO() {
cout << "(" << I << "," << J << ") ";
LOOP<I+1, J>::DO();
}
};
template<int J>
class INNER<M, J> {
public:
static inline void DO() {
LOOP<0, J+1>::DO();
}
};
template<int I>
class INNER<I, N> {
public:
static inline void DO() {
}
};
public:
static inline void DO() {
INNER<0, 0>::DO();
}
};

推荐答案

Robin Eidissen写道:
Robin Eidissen wrote:
我尝试的是什么做的是使用模板元编程迭代两个变量。我已经专门做了这样的事情,当它到达终点时,它会在下一行开始,当它到达最后一行时它会停止......
至少那就是我的想法但是,VC71说警告C4717:
''LOOP< 0,1> :: DO'':在所有控制路径上递归,函数将导致运行时堆栈溢出。
怎么了?

这里是代码:
模板< int M,int N>
类LOOP {
私有:
template< int I,int J>
class INNER {
public inline void DO(){
cout<< (<<<<<","<< J<")" ;;
LOOP< I + 1,J> :: DO( );
}
};
模板< int J>
类INNER< M,J> {
public:
static inline void DO(){
LOOP< 0,J + 1> :: DO();
}
};
template< int I>
类INNER< I,N> {
公开:
static inline void DO(){
}
};
public:
static inline void DO(){
INNER< 0,0> :: DO();
}
};
What I try to do is to iterate over two variables using template
metaprogramming. I''ve specialized it such that when it reaches the end
of a row ot starts on the next and when it reaches the last row it stops..
At least that''s what I thought I did, but VC71 says "warning C4717:
''LOOP<0,1>::DO'' : recursive on all control paths, function will cause
runtime stack overflow".
What''s wrong?

Here''s the code:
template<int M, int N>
class LOOP {
private:
template<int I, int J>
class INNER {
public:
static inline void DO() {
cout << "(" << I << "," << J << ") ";
LOOP<I+1, J>::DO();
}
};
template<int J>
class INNER<M, J> {
public:
static inline void DO() {
LOOP<0, J+1>::DO();
}
};
template<int I>
class INNER<I, N> {
public:
static inline void DO() {
}
};
public:
static inline void DO() {
INNER<0, 0>::DO();
}
};



我的上帝真是个可怕的错误!我再次调用LOOP而不是INNER!


Oh my god what a horrible mistake! I call LOOP again instead of INNER!


但它仍然无法正常工作。

On" LOOP< 3,3> :: DO();"它输出(0,1)(0,2)(1,2)这肯定是错误的b $ b。似乎在合适的时间没有调用专业化。
But it still won''t work correctly.
On "LOOP<3, 3>::DO();" it outputs "(0,1) (0,2) (1,2)" which is decidedly
wrong. It seems that the specializations aren''t invoked at the right times.


Robin Eidissen写道:
Robin Eidissen wrote:
但它仍然不起作用正确。
On" LOOP< 3,3> :: DO();"它输出(0,1)(0,2)(1,2)这显然是错误的。似乎在合适的时间没有调用专业化。
But it still won''t work correctly.
On "LOOP<3, 3>::DO();" it outputs "(0,1) (0,2) (1,2)" which is decidedly
wrong. It seems that the specializations aren''t invoked at the right times.




确保你使用正确的编译器来完成这项工作。 VC ++ v6是

在模板方面不符合要求。


V



Make sure you''re using the right compiler for the job. VC++ v6 is
not up to snuff when it comes to templates.

V


这篇关于2变量“嵌套”用TMP循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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