user4754284
user4754284

Reputation:

Repeating macro n times

I want to ask if there's some way, to "repeat" macro n times automatically - by automatically I mean compile time, I want to do something like this:

#define foo _asm mov eax, eax
#define bar(x) //I don't know how can I do it
int main()
{
    bar(5); //would generate 5 times _asm mov eax, eax
    return 0;
}

I know I can embed macros in other macros but I don't know how can I do it something exactly n times. I want to use it in random-sized junk generator

Upvotes: 2

Views: 840

Answers (1)

user2556165
user2556165

Reputation:

You can do this using recoursive template:

// recoursive step
template
  <
    size_t   count
  >
void n_asm() {
  _asm mov eax, eax
  n_asm<count - 1>();
}

// base of recursion
template<>
void n_asm<0>() {

}

int main()
{
   n_asm<5>(); 

   return 0;
}

Upvotes: 1

Related Questions