簡體   English   中英

傳遞包含特殊字符的預處理器標志 [c++17, g++]

[英]Passing pre-processor flags that contain special characters [c++17, g++]

我試圖通過以下方式使用預處理器標志為const char*賦值。

#include <iostream>

#define ST(A...) #A
#define STR(A) ST(A)

const char* output = STR(FLAG);

int main() {
    std::cout << output;
}

這種方法效果很好,除非 FLAG 包含一些特殊字符。 當我嘗試用

g++ .\Main.cpp -std=c++17 -DFLAG="a,b!c)d}e-f"

我在錯誤日志中得到了這個。 它不喜歡特殊字符。

<command-line>: error: expected ',' or ';' before 'd'
.\Main.cpp:4:19: note: in definition of macro 'STR'
    4 | #define STR(A) ST(A)
      |                   ^
.\Main.cpp:6:26: note: in expansion of macro 'FLAG'
    6 | const char* output = STR(FLAG);
      |                          ^~~~
<command-line>: error: expected declaration before '}' token
.\Main.cpp:4:19: note: in definition of macro 'STR'
    4 | #define STR(A) ST(A)
    6 | const char* output = STR(FLAG);
      |                          ^~~~
<command-line>: error: 'e' does not name a type
.\Main.cpp:4:19: note: in definition of macro 'STR'
    4 | #define STR(A) ST(A)
      |                   ^
.\Main.cpp:6:26: note: in expansion of macro 'FLAG'
    6 | const char* output = STR(FLAG);
      |  

該程序所需的 output 將是a,b!c)d}ef

是否有任何(理智的)方法可以使用預處理器標志或類似的東西來獲得此功能?

FLAG是一個字符串時,您不必使用字符串化宏:

-std=c++17 -DFLAG="\"a,b!c)d}e-f\""

現場演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM