在 C++ 中允许单行注释的多行宽字符串常量
Multi-line wide string constant that allows single line comments in C++
这个关于如何在代码中沿多行拆分(宽)字符串常量的问题已在此平台上被多次询问和回答(例如,here)。我一直在使用以下方法在 class 定义本身中声明静态宽字符串:
#include <Windows.h>
#include <iostream>
class Test
{
public:
static constexpr WCHAR TEST[] = L"The quick brown fox" \
L"jumped over the" \
L"lazy dog!";
/* ... */
};
int main ()
{
wprintf(Test::TEST);
return 1;
}
但是,如果您想开始对多行字符串的每一行进行注释,则此方法效果不佳。实现此目的的唯一方法是在字符串和反斜杠 (\
) 之间添加多行注释,如下所示:
class Test
{
public:
static constexpr WCHAR TEST[] = L"The quick brown fox" /* A comment */\
L"jumped over the" /* Another comment*/\
L"lazy dog!";
/* ... */
};
如果在反斜杠后添加注释 (\
),则会引发编译器错误。这意味着,您不能在此方法中使用传统的单行注释 (//
)。事实上,如果您在反斜杠(\
)后放置一个 space,则会产生编译错误。
我的问题如下,有没有办法在多行上声明一个宽字符串,您可以在其中使用单行注释 (//
) 来注释每一行?
我正在闲着找这样的东西(类似于Java):
static constexpr ... = L"The quick brown fox" + // A little comment here...
L"jumped over the" + // A little comment there.
L"lazy dog!";
我知道 Java 中的事情非常不同(即一切都是对象),但我只是举这个例子来说明我所追求的。
只需删除反斜杠:
static constexpr WCHAR TEST[] = L"The quick brown fox" // a comment
L"jumped over the" // another comment
L"lazy dog!"; // yet another comment
注释在翻译阶段 3 中被 space 个字符替换。相邻的字符串文字在翻译阶段 6 中连接在一起。
这个关于如何在代码中沿多行拆分(宽)字符串常量的问题已在此平台上被多次询问和回答(例如,here)。我一直在使用以下方法在 class 定义本身中声明静态宽字符串:
#include <Windows.h>
#include <iostream>
class Test
{
public:
static constexpr WCHAR TEST[] = L"The quick brown fox" \
L"jumped over the" \
L"lazy dog!";
/* ... */
};
int main ()
{
wprintf(Test::TEST);
return 1;
}
但是,如果您想开始对多行字符串的每一行进行注释,则此方法效果不佳。实现此目的的唯一方法是在字符串和反斜杠 (\
) 之间添加多行注释,如下所示:
class Test
{
public:
static constexpr WCHAR TEST[] = L"The quick brown fox" /* A comment */\
L"jumped over the" /* Another comment*/\
L"lazy dog!";
/* ... */
};
如果在反斜杠后添加注释 (\
),则会引发编译器错误。这意味着,您不能在此方法中使用传统的单行注释 (//
)。事实上,如果您在反斜杠(\
)后放置一个 space,则会产生编译错误。
我的问题如下,有没有办法在多行上声明一个宽字符串,您可以在其中使用单行注释 (//
) 来注释每一行?
我正在闲着找这样的东西(类似于Java):
static constexpr ... = L"The quick brown fox" + // A little comment here...
L"jumped over the" + // A little comment there.
L"lazy dog!";
我知道 Java 中的事情非常不同(即一切都是对象),但我只是举这个例子来说明我所追求的。
只需删除反斜杠:
static constexpr WCHAR TEST[] = L"The quick brown fox" // a comment
L"jumped over the" // another comment
L"lazy dog!"; // yet another comment
注释在翻译阶段 3 中被 space 个字符替换。相邻的字符串文字在翻译阶段 6 中连接在一起。