了解延迟宏

Understanding deferring macro

提问人:rishabh thakkar 提问时间:3/28/2023 最后编辑:johnrishabh thakkar 更新时间:3/28/2023 访问量:55

问:

我正在尝试理解延迟的宏层次结构。这有意义吗?

// Online C++ compiler to run C++ program online
#include <iostream>
#define EMPTY()
#define EVAL_1(...) __VA_ARGS__
#define EVAL_2(...) EVAL_1(EVAL_1(__VA_ARGS__))

#define DEFER(...) __VA_ARGS__ EMPTY()
#define DEFER2(...) __VA_ARGS__ DEFER(EMPTY) ()
#define DEFER3(...) __VA_ARGS__ DEFER2(EMPTY) ()
#define DEFER4(...) __VA_ARGS__ DEFER3(EMPTY) ()
#define A(x) x

int main() {
    // The parameter is evaluated during expansion.
    std::cout << (EVAL_1(DEFER(A)(5)))<<std::endl; //line 1: evaluate(A (5)) = 5. Output = 5  
    //std::cout << (EVAL_1(DEFER2(A)(5)))<<std::endl; // line 2: evaluate(A EMPTY() (5)) = A(5), Output = error.
    //std::cout << (EVAL_1(DEFER3(A)(5)))<<std::endl; // line 3: evaluate(A EMPTY EMPTY() () (5)) = A EMPTY() (5), Output = error.
    //std::cout << (EVAL_1(DEFER4(A)(5)))<<std::endl; // line 4: evaluate(A EMPTY EMPTY EMPTY() () () (5)) = A EMPTY EMPTY () () (5), Output = error.

    
    std::cout << (EVAL_2(DEFER(A)(5)))<<std::endl; //line 5: evaluate(evaluate(5)) from line 1. Output = 5.
    std::cout << (EVAL_2(DEFER2(A)(5)))<<std::endl;//line 6: evaluate(evaluate(A(5))) = 5. Output =5
    std::cout << (EVAL_2(DEFER3(A)(5)))<<std::endl;//line 7: evaluate(evaluate(A EMPTY() (5)))=5.
    //std::cout <<(EVAL_2(DEFER4(A)(5)))<<std::endl;//line8: error

    return 0;
}

是否有任何我遗漏或误解的微妙特征?

C++ 宏延迟 预处理器指令

评论

0赞 Some programmer dude 3/28/2023
您是否查看了预处理的代码以了解宏是如何扩展的?
0赞 Some programmer dude 3/28/2023
你想做什么?这些宏应该解决什么问题?你为什么要写这样的宏?或者你从哪里得到它们?
0赞 john 3/28/2023
对于那些感兴趣的人,这似乎是来源,请参阅“延迟表达式”部分

答: 暂无答案