문제

매크로 확장을 추적하고 싶습니다. 매크로가 확장 되었는 몇 번이나 확장이 발생했을 때 Args는 무엇입니까?

예를 들어

매크로가 다음과 같이 보일 수 있습니다 :

#define mymacro(x) int x
.

및 내 코드에서 다음과 같은 것이 있습니다.

mymacro(a);
mymacro(b);
.

전처리 기 확장이 끝나면 (오, 그래, 특정 매크로가 확장 할 수있는 특정 매크로가 될 수있는 방법이 있습니까?), 나는 몇 번의 mymacro가 사용되었는지 알고 싶습니다....에이 경우 2 번이고 Args는 A와 B가 될 것입니다.

나는 부스트 전처리 자 (boost-preprocessor) 라이브러리를 조사하고 있었다.그들은 boost_pp_array를 가지고 있지만, 나중에 사용할 수 있도록 "정적"을 만드는 방법을 모르겠습니다.

boost_pp_counter에서 뭔가를 발견했습니다.BOOST_PP_COUTTER가 전 처리기 구문에서 상태를 유지할 수있는 것 같습니다.그러나 나는 내가 원하는 것을하는 방법을 여전히 불분명합니다.

도움이 되었습니까?

해결책

How about something like this?

#include <iostream>

int m_counter = 0;
const char *m_arguments[32] = { 0 };

#define COUNT_M(a) m_arguments[m_counter++] = #a;
#define M(a) COUNT_M(a) int a

int main()
{
    M(x);
    M(y);

    for (int i = 0; i < m_counter; i++)
    {
        std::cout << "m_arguments[" << i << "] = \"" << m_arguments[i] << "\"\n";
    }
}

다른 팁

I'm not quite sure what your ultimate goal is, but you can track the number of scans(not the number of expansions) using an active argument. An active argument expands each time that it is scanned by the preprocessor. For example,

#define EMPTY()

#define A(n) \
    A_INDIRECT EMPTY()()(BOOST_PP_INC(n)) 

#define A_INDIRECT() A

#define X(arg) arg
#define Y(arg) X(arg)
#define Z(arg) Y(arg)

   A(0)   // A_INDIRECT()(1)
X( A(0) ) // A_INDIRECT()(2)
Y( A(0) ) // A_INDIRECT()(3)
Z( A(0) ) // A_INDIRECT()(4)

Each invocation of A is subjected to a different number of scans, which causes the results to be different each time.

Macros can't effect the global state. The only other way to achieve some sort of state is to use recursion. Remember, macros don't expand recursively, so the preprocessor keeps tracks of this state. Its the only "global" state that can be affected by macros. However, it can be difficult to control. Macros have to be forced to expand at a certain recursion level with a macro for each level, and some form of binary searching is required to read the "state" efficiently.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top