Препроцессор C ++: отложите вызов макроса позже - PullRequest
0 голосов
/ 02 декабря 2018

Я сейчас создаю свой макрос for_each.Я нашел какой-то пример переполнения стека, поэтому черпаю вдохновение у них, но хочу понять все, что я делаю, поэтому я пришел задавать вопросы ^^

Я пытаюсь выполнить рекурсию «остановки»,Поэтому я пишу этот код:

#include <iostream>

#define PRINT_SQUARE(x) std::cout << (x) * (x) << std::endl;

#define END(...)

#define MAP_END(...) 0, END
#define MAP_NEXT0(item, next, ...) next
#define MAP_NEXT(item, next) MAP_NEXT0(0, END, next) // This part for test

#define MAP_1(f, x, end) f(x) MAP_NEXT(end, MAP_0)(f, 0)

#define MAP(f, x, y) MAP_1(f, x, ())

int main() {
  MAP(PRINT_SQUARE, 5, 8);

  return 0;
}

Этот код работает.Однако у меня есть эта строка #define MAP_NEXT(item, next) MAP_NEXT0(0, END, next)

Поэтому вместо того, чтобы сделать это, я попытался заменить 0, END вызовом макроса MAP_END, но безуспешно ...

#include <iostream>

using namespace std;

#define PRINT_SQUARE(x) std::cout << (x) * (x) << std::endl;

#define FORCE_EXPANDING(x) x

#define END(...)

#define MAP_END(...) 0, END
#define MAP_NEXT0(item, next, ...) next
#define MAP_NEXT(item, next) MAP_NEXT0(MAP_END(), next)

#define MAP_1(f, x, end) f(x) MAP_NEXT(end, MAP_0)(f, 0)

#define MAP(f, x, y) MAP_1(f, x, ())

int main() {
  MAP(PRINT_SQUARE, 5, 8);

  return 0;
}

Не компилируется:

..\main.cpp(20): error C2065: 'PRINT_SQUARE': undeclared identifier
..\main.cpp(20): error C3861: 'MAP_0': identifier not found
...