为什么允许空 wchar_t 文字?
看下面的代码:
int main(int argc, char* argv[])
{
// This works: (Disable Lang Ext = *Yes* (/Za))
wchar_t wc0 = L'\0';
wchar_t wc_ = L'';
assert(wc0 == wc_);
// This doesn't compile (VC++ 2010):
char c0 = '\0';
char c_ = ''; // error C2137: empty character constant
assert(c0 == c_);
return 0;
}
为什么编译器允许为宽字符定义空字符文字?这对于 Wide 没有意义,就像对于编译器标记错误的 char 没有意义一样。
标准允许这样做吗?
Look at the following code:
int main(int argc, char* argv[])
{
// This works: (Disable Lang Ext = *Yes* (/Za))
wchar_t wc0 = L'\0';
wchar_t wc_ = L'';
assert(wc0 == wc_);
// This doesn't compile (VC++ 2010):
char c0 = '\0';
char c_ = ''; // error C2137: empty character constant
assert(c0 == c_);
return 0;
}
Why does the compiler allow defining an empty character literal for wide characters? This doesn't make sense for wide, just as it doesn't make sense for char
where the compiler flags an error.
Is this allowed by the Standard?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 VC++ 中的一个错误< /a>.
This is a bug in VC++.
ISO 标准不允许。这是微软产品中的一个错误。即使他们的描述该特定功能的页面也没有提及这种异常(或可憎的,取决于你的观点)行为。
字符文字的定义(取自 C++0x 的
2.14.3
,但相关位在 C++03 中没有变化)包含:如您所见,没有 这样,
L'x'
中的'
字符之间就不会出现任何内容。它必须是一个或多个c_char
字符。事实上,这一点在下面的段落中已经明确(我的重点):It is not allowed per the ISO standard. This is a bug in Microsoft's product. Even their page describing that particular feature makes no mention of this aberrant (or abhorrent, depending on your viewpoint) behaviour.
The definition for a character literal (as taken from
2.14.3
of C++0x but the relevant bit is unchanged from C++03) contains:As you can see, there is no way that you can end up with nothing between the
'
characters inL'x'
. It has to be one or more of thec_char
characters. In fact, this is made explicit in the following paragraph (my emphasis):我认为,根据 C++ 标准 2.23.2.1,第一个示例是不允许的:
(强调我的。)
I would argue that the first example is not allowed, per 2.23.2.1 of the C++ standard:
(Emphasis mine.)