473,624 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

2 variable "nested" loop with TMP

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();
}
};
Jul 22 '05 #1
15 2525
Robin Eidissen wrote:
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();
}
};

Oh my god what a horrible mistake! I call LOOP again instead of INNER!
Jul 22 '05 #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.
Jul 22 '05 #3
Robin Eidissen wrote:
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.


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

V
Jul 22 '05 #4
Victor Bazarov wrote:
Robin Eidissen wrote:
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.

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

V

I use Visual C++ 2003.
Jul 22 '05 #5
Robin Eidissen wrote in news:c8******** **@orkan.itea.n tnu.no in
comp.lang.c++:
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 { public:
static inline void DO() {
inline here is unnessacery function's defined inside a class
are always inline.
};


With some correction's I got your version to work with an EDG compiler
but I couldn't be bothered wating for VC 7.1 to run out of memory,
g++ (3.4), didn't compile it either.

This seems to work though:

#include <iostream>
#include <ostream>

template< int M, int N, int I = M, int J = N >
struct loop
{
template < typename F >
static void apply( F f )
{
f( M - I, N - J );
loop<M, N, I, J - 1>::apply( f );
}
};

template< int M, int N, int I >
struct loop< M, N, I, 0 >
{
template < typename F >
static void apply( F f )
{
loop<M, N, I - 1, N>::apply( f );
}
};

template< int M, int N, int J >
struct loop< M, N, 0, J >
{
template < typename F >
static void apply( F )
{
}
};
void function( int i, int j )
{
std::cout << "(" << i << "," << j << ") ";
}

int main()
{
loop<3, 3>::apply( function );
}

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #6
Robin Eidissen wrote:
Victor Bazarov wrote:
Robin Eidissen wrote:
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.


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

V


I use Visual C++ 2003.


Please post the final code that you have, describe the output you get
and the output you would like to get. Otherwise, I am lost trying to
merge your original (apparently incorrect) code and the corrections
you described in replies to yourself.

Thanks.

V
Jul 22 '05 #7
#include <iostream>
using namespace std;

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

int main() {
LOOP<3, 3>::DO();
return 0;
}

I want this to output "(0,0) (1,0) (2,0) (0,1) (1,1) (2,1) (0,2) (1,2)
(2,2)". What is does output is: "(0,1) (0,2) (1,2)".
Jul 22 '05 #8
Thanks that worked very nicely! But for "educationa l purposes" I'd
appreciate it if anyone can help me out with finding the error in the
latest version I posted.
Jul 22 '05 #9
By the way, if I put "inline" in front of the "function" declaration,
will the entire thing be neatly unrolled with no function calls?
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3915
by: Dave | last post by:
I have to automate a process that assigns sales leads to sales people. For example: Every day we buy a list of sales leads, it ranges in size from 50 - 100 records. We have a team of sales people that also can range from 5 - 8 people. I need to take the new records and divide them evenly among the sales people.
3
3348
by: r rk | last post by:
I am trying to write a utility/query to get a report from a table. Below is the some values in the table: table name: dba_daily_resource_usage_v1 conn|loginame|dbname|cum_cpu|cum_io|cum_mem|last_batch ------------------------------------------------------------ 80 |farmds_w|Farm_R|4311 |88 |5305 |11/15/2004 11:30 80 |abcdes_w|efgh_R|5000 |88 |4000 |11/15/2004 12:30 45 |dcp_webu|DCP |5967 |75 |669 |11/16/2004...
8
2827
by: Etienne Boucher | last post by:
Nested classes are usualy objects made to only live in their parent object instance. In other words... public class Outter { public class Inner { } }
1
1962
by: Roy | last post by:
Hey all. Below is the nested syntax on how to make a "codeless" nested gridview embedded within another gridviews templatefield column. Only problem is that it loads slow. REAL SLOW. There has to be a better way. Suggestions anyone? By the way, I'm not opposed to coding, it just seems like this should be easily doable on the aspx side of things. Summary: I'm stuffing the 3 three key fields from each row in the master gridview into...
1
1578
by: Goldie | last post by:
Can anyone offer advice on how to do a nested loop with vb I need the loop nested in the main loop to be passed a variable from the parent loop for SQL purposes. eg: parent selects all customer_id's and other information child loop needs the customer_id to do SELECT * FROM table WHERE customer_id = '@from_parent_loop'
4
1710
by: MDR | last post by:
Hello I have three "for" loops, two nested into the outer one and they depend on each other, like this: for (x=1; x<100; x++) { .... for (i=1; i<10; i++) {....} for (j=1; j<10; j++)
18
3632
by: desktop | last post by:
I have 3 types of objects: bob1, bob2 and bob3. Each object is identified by a unique ID which gets returned by the function getId(). All bobs are descendants from class BaseBob which is an abstract class that has the virtual function getId(). I then have a function that prints a special string when a bob meets another bob (all combinations of bobs has to meet and since the are schizophrenic they can also meet themselves):
3
1468
by: bennie72 | last post by:
need to script a "*" pattern in nested loop? ********************** ********************** ********* ********* ********************* ********************* ********* *********
8
2737
by: phub11 | last post by:
Hi - I have a function which appends row(s) to the bottom of a table: function mouseUpHandler() { var table = document.getElementById("mytable"); var rowCount = table.rows.length; var addRow = table.insertRow(rowCount); addRow.id = cnt; var addCell = addRow.insertCell(1); addCell.id = cnt;
0
8619
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8334
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8474
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6108
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2604
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.