473,379 Members | 1,255 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Metaprogramming "for" loop

Hi,

I have the code below this email. I want to replace the last 4 lines
with a Metaprogramming loop to get something like the following (I
don't know the syntax). Is it possible?

for type in {left_tag, right_tag, down_tag, up_tag) {
fun(type());
}
Thanks,
Peng

struct left_tag{};
struct right_tag{};
struct down_tag{};
struct up_tag{};

template <typename Dir>
void fun(Dir) { }

//Can I replace the following code with a metaprogramming "for" loop
fun(left_tag());
fun(right_tag());
fun(down_tag());
fun(up_tag());

Feb 25 '07 #1
9 3396
On 25 Feb, 02:32, "PengYu...@gmail.com" <PengYu...@gmail.comwrote:
Hi,

I have the code below this email. I want to replace the last 4 lines
with a Metaprogramming loop to get something like the following (I
don't know the syntax). Is it possible?

for type in {left_tag, right_tag, down_tag, up_tag) {
fun(type());

}

Thanks,
Peng

struct left_tag{};
struct right_tag{};
struct down_tag{};
struct up_tag{};

template <typename Dir>
void fun(Dir) { }

//Can I replace the following code with a metaprogramming "for" loop
fun(left_tag());
fun(right_tag());
fun(down_tag());
fun(up_tag());
Yes.

However in all the metaprogramming libraries I know of you need to
put the tags in some sort of tuple which is accessible to
metaprogramming. (std::tr1::tuple could be adapted for this but no std
way exists AFAIK) In my own libraries I would do something like the
following code below --->

Other libraries that do similar are boost::fusion (not released but
accessible via CVS). I think you can do similar with boost::mpl though
it is nominally only for compile time programming AFAIK and isnt
compatible with std::tr1 type_traits or std::tr1::tuple IIRC.

#include <quan/fun/vector4.hpp>
#include <quan/fun/for_each.hpp>

struct left_tag{};
struct right_tag{};
struct down_tag{};
struct up_tag{};

// some tuple with metaprogramming facilities
typedef quan::fun::vector4<left_tag,right_tag,down_tag,up_ tagtags;

// any functor with similar signature will do.

struct fun{

template <typename T>
void operator()(T const & t) const
{
std::cout << typeid(T).name() <<'\n';
}

};
int main()
{
quan::fun::for_each(tags(),fun());

}

/*
output:

struct left_tag
struct right_tag
struct down_tag
struct up_tag

*/

Feb 25 '07 #2
Pe*******@gmail.com wrote:
Hi,

I have the code below this email. I want to replace the last 4 lines
with a Metaprogramming loop to get something like the following (I
don't know the syntax). Is it possible?

for type in {left_tag, right_tag, down_tag, up_tag) {
fun(type());
}
Thanks,
Peng

struct left_tag{};
struct right_tag{};
struct down_tag{};
struct up_tag{};

template <typename Dir>
void fun(Dir) { }

//Can I replace the following code with a metaprogramming "for" loop
fun(left_tag());
fun(right_tag());
fun(down_tag());
fun(up_tag());
I am quoting one of my previous posts that may be helpful to you
regarding this. The idea is it define a tuple that has all your
structures in one place and let the template metaprogram unfold it
for you.

So in your case, you will need to define
typedef boost::tuple<left_tag,right_tag,down_tag,up_tagstu ffToProc;

then you do stuff similar to the metaprogram. Here I modified it to
call your function, fun()

// recursive case
template< int N >
class test
{
public:
static void doIt( const stuffToProc &t )
{
test<N-1>::doIt( t );
fun( t.get<N>() );
}
};

// basis case
template<>
class test<0>
{
public:
static void doIt( const stuffToProc &t )
{
fun( t.get<0>() );
}
};

then you can use it as follows:

int
main()
{
stuffToProc thisIsTheStuff;
test<4>::doit( thisIsTheStuff );
}

I was investigating a way to manufacture objects based
on an array of typeid's but I did not have the time.
I think that the problem you are describing can be
solved differently and probably easily if we can figure
out an efficient way to create arbitrary typed objects
from its typeid. When I get the chance, I'll investigate
this further since this problem keeps popping up now and
again.

HTH
Ok so let's look at boost::tuple. You can create essentially a structure-like object with it except that the accessors are via numbers.
(http://www.boost.org/libs/tuple/doc/...ers_guide.html)

Then you can use a recursive template metaprogram to access each
element in the tuple. So it would look like something like this:

// for illustration purposes only
typedef boost::tuple<int,float,doubleYourTuple;

// recursive case
template< int N >
class test
{
public:
static void doIt( const YourTuple &t )
{
test<N-1>::doIt( t );
reverseEndian<>( t.get<N>() );
}
};

// basis case
template<>
class test<0>
{
public:
static void doIt( const YourTuple &t )
{
reverseEndian<>( t.get<0>() );
}
};
Feb 25 '07 #3
On 25 Feb, 06:25, Piyo <cybermax...@yahoo.comwrote:

<...>

Thats basically it using boost::tuple..
You can wrap the implementation in some for_each construct, which
automatically computes the size and use more template params. For
boost tuple you can get the size by tuple::length, and dont forget its
zero based so off by 1.
It should then work for different element_types and tuple lengths
too...

regards Andy Little

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

struct left_tag{};
struct right_tag{};
struct down_tag{};
struct up_tag{};

typedef boost::tuple<left_tag,right_tag,down_tag,up_tagstu ffToProc;

struct fun{

template <typename T>
void operator()(T const & t)const
{
std::cout << typeid(T).name() <<'\n';
}
};

template< int N >
class test {
public:
template <typename T, typename F>
static void doIt( const T &t,F f )
{
test<N-1>::doIt( t ,f );
f( t. template get<N>() );
}
};

// basis case
template<>
class test<0{
public:
template <typename T, typename F>
static void doIt( const T &t, F f )
{
f( t. template get<0>() );
}
};

// some for_each
template <typename T, typename F>
void for_each( T & t, F f)
{
test<(boost::tuples::length<T>::value -1)>::doIt(t,f);
}
int main()
{
stuffToProc thisIsTheStuff;
for_each(thisIsTheStuff ,fun());
}

regards
Andy Little


Feb 25 '07 #4
On 25 Feb, 07:13, "kwikius" <a...@servocomm.freeserve.co.ukwrote:
On 25 Feb, 06:25, Piyo <cybermax...@yahoo.comwrote:

<...>

Thats basically it using boost::tuple..
You can wrap the implementation in some for_each construct, which
automatically computes the size and use more template params. For
boost tuple you can get the size by tuple::length, and dont forget its
zero based so off by 1.
It should then work for different element_types and tuple lengths
too...

regards Andy Little
Oh and you can use the make_tuple function so you dont need the
explicit typedef, which is pretty close to what you want I think :

int main()
{
// stuffToProc thisIsTheStuff;
for_each(
boost::tuples::make_tuple(
left_tag(),right_tag(),up_tag(),down_tag()
)
,fun());

// or ...
left_tag lt;
right_tag rt;
down_tag dt;
up_tag ut;

using boost::tuples::make_tuple;

for_each(make_tuple(lt,rt,ut,dt),fun());
regards
Andy Little

Feb 25 '07 #5
kwikius wrote:
>
int main()
{
// stuffToProc thisIsTheStuff;
for_each(
boost::tuples::make_tuple(
left_tag(),right_tag(),up_tag(),down_tag()
)
,fun());

// or ...
left_tag lt;
right_tag rt;
down_tag dt;
up_tag ut;

using boost::tuples::make_tuple;

for_each(make_tuple(lt,rt,ut,dt),fun());
regards
Andy Little
Pretty sweet stuff there Andy :) I was also thinking to
suggest dropping the class and make "test" into a function
template. I wrote that original code to solve something
else but eventually didn't (I couldn't figure out some
stuff).

On a different note: you brought up the use of tr1 data
structs. Do you know of a website detailing the stuff there?
I can't seem to find one that is as clean and organized
as sgi's website of the STL stuff.

Thanks!
Feb 25 '07 #6
In article <H0*************@newssvr19.news.prodigy.net>, cybermax_69
@yahoo.com says...

[ ... ]
On a different note: you brought up the use of tr1 data
structs. Do you know of a website detailing the stuff there?
I can't seem to find one that is as clean and organized
as sgi's website of the STL stuff.
I don't know of a web site, but Pete Becker's book (_The C++ Standard
Library Extensions_) is quite good.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 26 '07 #7
On 26 Feb, 00:35, Jerry Coffin <jcof...@taeus.comwrote:
In article <H0pEh.523$tv...@newssvr19.news.prodigy.net>, cybermax_69
@yahoo.com says...

[ ... ]
On a different note: you brought up the use of tr1 data
structs. Do you know of a website detailing the stuff there?
I can't seem to find one that is as clean and organized
as sgi's website of the STL stuff.

I don't know of a web site, but Pete Becker's book (_The C++ Standard
Library Extensions_) is quite good.
Yes, and you can find out the specs by looking at the draft C++
extensions paper:

http://www.open-std.org/jtc1/sc22/wg...2005/n1836.pdf
regards
Andy Little

Feb 26 '07 #8
On Feb 25, 1:24 am, "kwikius" <a...@servocomm.freeserve.co.ukwrote:
On 25 Feb, 02:32, "PengYu...@gmail.com" <PengYu...@gmail.comwrote:
Hi,
I have the code below this email. I want to replace the last 4 lines
with a Metaprogramming loop to get something like the following (I
don't know the syntax). Is it possible?
for type in {left_tag, right_tag, down_tag, up_tag) {
fun(type());
}
Thanks,
Peng
struct left_tag{};
struct right_tag{};
struct down_tag{};
struct up_tag{};
template <typename Dir>
void fun(Dir) { }
//Can I replace the following code with a metaprogramming "for" loop
fun(left_tag());
fun(right_tag());
fun(down_tag());
fun(up_tag());

Yes.

However in all the metaprogramming libraries I know of you need to
put the tags in some sort of tuple which is accessible to
metaprogramming. (std::tr1::tuple could be adapted for this but no std
way exists AFAIK) In my own libraries I would do something like the
following code below --->

Other libraries that do similar are boost::fusion (not released but
accessible via CVS). I think you can do similar with boost::mpl though
it is nominally only for compile time programming AFAIK and isnt
compatible with std::tr1 type_traits or std::tr1::tuple IIRC.

#include <quan/fun/vector4.hpp>
#include <quan/fun/for_each.hpp>

struct left_tag{};
struct right_tag{};
struct down_tag{};
struct up_tag{};

// some tuple with metaprogramming facilities
typedef quan::fun::vector4<left_tag,right_tag,down_tag,up_ tagtags;

// any functor with similar signature will do.

struct fun{

template <typename T>
void operator()(T const & t) const
{
std::cout << typeid(T).name() <<'\n';
}

};

int main()
{
quan::fun::for_each(tags(),fun());

}

/*
output:

struct left_tag
struct right_tag
struct down_tag
struct up_tag

*/
Could you please show me what are in the files quan/fun/vector4.hpp
quan/fun/for_each.hpp?

Thanks,
Peng

Mar 14 '07 #9
On Feb 26, 12:27 am, "kwikius" <a...@servocomm.freeserve.co.ukwrote:
On 26 Feb, 00:35, Jerry Coffin <jcof...@taeus.comwrote:
In article <H0pEh.523$tv...@newssvr19.news.prodigy.net>, cybermax_69
@yahoo.com says...
[ ... ]
On a different note: you brought up the use of tr1 data
structs. Do you know of a website detailing the stuff there?
I can't seem to find one that is as clean and organized
as sgi's website of the STL stuff.
I don't know of a web site, but Pete Becker's book (_The C++ Standard
Library Extensions_) is quite good.

Yes, and you can find out the specs by looking at the draft C++
extensions paper:

http://www.open-std.org/jtc1/sc22/wg...2005/n1836.pdf

regards
Andy Little
Are these implemented in any compiler, yet?

Tahnks,
Peng

Mar 14 '07 #10

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

Similar topics

23
by: Invalid User | last post by:
While trying to print a none empty list, I accidentaly put an "else" statement with a "for" instead of "if". Here is what I had: if ( len(mylist)> 0) : for x,y in mylist: print x,y else:...
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
3
by: songie D | last post by:
would it be possible to sort of engineer some sort of preprocessor macro that does a 'for' loop. i.e. for where you would normally use a normal for loop, but when it is known ay compile time whay...
12
by: Robbie Hatley | last post by:
I'm getting a bizarre error with this code: ... case WM_COMMAND: { switch (LOWORD(wParam)) // switch (control ID) { ... case IDC_RAIN_ENABLE_SIMPLE: {
5
by: Fabian Vilers | last post by:
Hi again... I'm wondering what could be better in terms of performance between: var my_array = new Array(); // populate array for (index in my_array) { // do something with my_array
10
by: s.lipnevich | last post by:
Hi All, I apologize if this was brought up before, I couldn't find any "prior art" :-). On more than one occasion, I found myself wanting to use a "conditional loop" like this (with "Invalid...
3
by: Patrick Sullivan | last post by:
In this for loop, IE skips over the animation function until the end of the loop and only aninmates the last phrase. Firefox does it right. Loop is right below, entire script is below that. This...
34
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include...
15
by: Steve | last post by:
I am having problems getting values out of an array. The array is set as a global array and values are pushed into it as they are read from a JSON file using a "for loop". When the "for loop" is...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.