我有以下問題:
#define CONCAT_(A,B) A ## B
#define CONCAT(A,B) CONCAT_(A,B)
#define CREATE_NAME(N) CONCAT(N, __COUNTER__)
如果我想稍后在代碼中檢索特定內容variable##__COUNTER__
,我該如何實作?我只需要獲得前一個,例如:
#define CONCAT_(A,B) A ## B
#define CONCAT(A,B) CONCAT_(A,B)
#define CREATE_NAME(N) CONCAT(N, __COUNTER__)
#define GET_NAME_PREV(N, VAL) CONCAT(N, VAL)
auto CREATE_NAME(v);
auto test_current_counter_value = GET_NAME_PREV(v, __COUNTER__ -1);
謝謝你。
uj5u.com熱心網友回復:
BOOST_PP_SUB
boost
可以評估庫中的宏并將其擴展為識別符號。
#include <boost/preprocessor/arithmetic/sub.hpp>
#define CONCAT_(A,B) A ## B
#define CONCAT(A,B) CONCAT_(A,B)
#define CREATE_NAME(N) CONCAT(N, __COUNTER__)
#define GET_NAME_PREV(N) CONCAT(N, BOOST_PP_SUB(__COUNTER__, 1))
auto CREATE_NAME(v) = true;
auto test_current_counter_value = GET_NAME_PREV(v);
在編譯器資源管理器上嘗試。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/469960.html